package com.fs.his.controller;

import java.util.List;

import com.fs.common.core.domain.R;
import com.fs.common.core.domain.model.LoginUser;
import com.fs.common.exception.ServiceException;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.StringUtils;
import com.fs.framework.web.service.TokenService;
import com.fs.his.dto.FsPhysicalReportTemplateFieldDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.enums.BusinessType;
import com.fs.his.domain.FsPhysicalReportTemplateField;
import com.fs.his.service.IFsPhysicalReportTemplateFieldService;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.common.core.page.TableDataInfo;

/**
 * 体检报告模板字段Controller
 *
 * @author fs
 * @date 2025-07-24
 */
@RestController
@RequestMapping("/his/physicalReportTemplateField")
public class FsPhysicalReportTemplateFieldController extends BaseController {
    @Autowired
    private IFsPhysicalReportTemplateFieldService fsPhysicalReportTemplateFieldService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询体检报告模板字段列表
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        startPage();
        List<FsPhysicalReportTemplateField> list = fsPhysicalReportTemplateFieldService.selectFsPhysicalReportTemplateFieldList(fsPhysicalReportTemplateField);
        return getDataTable(list);
    }

    /**
     * 导出体检报告模板字段列表
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:export')")
    @Log(title = "体检报告模板字段", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        List<FsPhysicalReportTemplateField> list = fsPhysicalReportTemplateFieldService.selectFsPhysicalReportTemplateFieldList(fsPhysicalReportTemplateField);
        ExcelUtil<FsPhysicalReportTemplateField> util = new ExcelUtil<FsPhysicalReportTemplateField>(FsPhysicalReportTemplateField.class);
        return util.exportExcel(list, "体检报告模板字段数据");
    }

    /**
     * 获取体检报告模板字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(fsPhysicalReportTemplateFieldService.selectFsPhysicalReportTemplateFieldById(id));
    }

    /**
     * 新增体检报告模板字段
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:add')")
    @Log(title = "体检报告模板字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        return toAjax(fsPhysicalReportTemplateFieldService.insertFsPhysicalReportTemplateField(fsPhysicalReportTemplateField));
    }

    /**
     * 修改体检报告模板字段
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:edit')")
    @Log(title = "体检报告模板字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsPhysicalReportTemplateField fsPhysicalReportTemplateField) {
        return toAjax(fsPhysicalReportTemplateFieldService.updateFsPhysicalReportTemplateField(fsPhysicalReportTemplateField));
    }

    /**
     * 删除体检报告模板字段
     */
    @PreAuthorize("@ss.hasPermi('his:physicalReportTemplateField:remove')")
    @Log(title = "体检报告模板字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(fsPhysicalReportTemplateFieldService.deleteFsPhysicalReportTemplateFieldByIds(ids));
    }

    /**
     * 获取模板自定义字段
     *
     * @param templateId 模板ID
     * @return R
     **/
    @PostMapping("/getTemplateField/{templateId}")
    public R getTemplateField(@PathVariable String templateId) {
        if (StringUtils.isNull(templateId)) {
            return R.error("操作失败，模板ID不能为空！");
        }
        return fsPhysicalReportTemplateFieldService.getTemplateField(templateId);
    }

    /**
     * 新增模板
     *
     * @param fieldDTO
     * @return AjaxResult
     **/
    @Log(title = "体检报告模板字段", businessType = BusinessType.INSERT)
    @PostMapping("/saveTemplate")
    public AjaxResult saveTemplate(@RequestBody FsPhysicalReportTemplateFieldDTO fieldDTO) {
        if (StringUtils.isNull(fieldDTO.getTemplateId())) {
            throw new ServiceException("操作失败,模板ID不能为空！");
        } else if (fieldDTO.getTemplateFieldList().isEmpty()) {
            throw new ServiceException("操作失败,字段列表不能为空！");
        }
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        fieldDTO.setCreateBy(String.valueOf(loginUser.getUserId()));
        return toAjax(fsPhysicalReportTemplateFieldService.saveFsPhysicalReportTemplateField(fieldDTO));
    }
}
