package com.hxhq.business.controller;

import java.util.List;

import com.hxhq.business.domain.*;
import com.hxhq.business.dto.study.StudyFormFillListDto;
import com.hxhq.business.form.study.StudyFormFillGcForm;
import com.hxhq.business.form.study.StudyFormFillShfzForm;
import com.hxhq.business.form.study.StudyFormFillSearchForm;
import com.hxhq.business.form.study.StudyFormUpdateForm;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.pdf.PdfExportUtil;
import com.hxhq.common.security.annotation.Logical;
import com.hxhq.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;


/**
 * 试验-填报单Controller
 *
 * @author hxhq
 * @date 2025-12-29
 */
@RestController
@RequestMapping("/business/studyFormFill")
public class StudyFormFillController extends BaseController {
    @Autowired
    private IStudyFormFillService studyFormFillService;
    @Autowired
    private IStudyFormFillJcgjService studyFormFillJcgjService;
    @Autowired
    private IStudyFormFillQmxxService studyFormFillQmxxService;
    @Autowired
    private ITemplateService templateService;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions(value = {"business:studyFormFill:list", "business:nonTrialFormFill:list", "business:drugFormFill:list"}, logical = Logical.OR)
    public TableDataInfo list(StudyFormFillSearchForm form) {
        startPage();
        List<StudyFormFillListDto> list = studyFormFillService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 稽查轨迹列表
     */
    @GetMapping("/jcgjList")
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    public TableDataInfo jcgjList(StudyFormFillJcgj form) {
        startPage();
        List<StudyFormFillJcgj> list = studyFormFillJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 签名信息列表
     */
    @GetMapping("/qmxxList")
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    public TableDataInfo qmxxList(StudyFormFillQmxx form) {
        startPage();
        List<StudyFormFillQmxx> list = studyFormFillQmxxService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 详细
     */
    @GetMapping(value = "/info")
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(studyFormFillService.queryInfo(id));
    }


    /**
     * 导出
     */
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    @GetMapping(value = "/exportDetail")
    public AjaxResult exportDetail(StudyFormFillJcgj form) {
        List<StudyFormFillJcgj> studyFormFillJcgjList = studyFormFillJcgjService.queryList(form);
        StudyFormFillQmxx studyFormFillQmxx = new StudyFormFillQmxx();
        studyFormFillQmxx.setFormId(form.getFormId());
        List<StudyFormFillQmxx> studyFormFillQmxxList = studyFormFillQmxxService.queryList(studyFormFillQmxx);
        StudyFormFill studyFormFill = studyFormFillService.queryInfo(form.getFormId());
        return AjaxResult.success(localFilePrefix + PdfExportUtil.export(studyFormFill.getTemplateExportClass(), studyFormFill.getTemplateExportMethod(), studyFormFill, studyFormFillQmxxList, studyFormFillJcgjList, form.getLang(), form.getJcgjlx(), localFilePath));
    }

    /**
     * 加签
     */
    @PostMapping("/jq")
    @RequiresPermissions(value = {"business:studyFormFill:jq", "business:nonTrialFormFill:jq", "business:drugFormFill:jq"}, logical = Logical.OR)
    public AjaxResult jq(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.jq(studyFormFill);
        return AjaxResult.success("操作成功");
    }

    /**
     * 废止
     */
    @PostMapping("/fz")
    @RequiresPermissions(value = {"business:studyFormFill:fz", "business:nonTrialFormFill:fz", "business:drugFormFill:fz"}, logical = Logical.OR)
    public AjaxResult fz(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.fz(studyFormFill);
        return AjaxResult.success("操作成功");
    }


    /**
     * 确认废止
     */
    @PostMapping("/qrfz")
    @RequiresPermissions(value = {"business:studyFormFill:qrfz", "business:nonTrialFormFill:qrfz", "business:drugFormFill:qrfz"}, logical = Logical.OR)
    public AjaxResult qrfz(@RequestBody StudyFormFillShfzForm form) {
        studyFormFillService.qrfz(form);
        return AjaxResult.success("操作成功");
    }


    /**
     * 更换归属人
     */
    @PostMapping("/ghgsr")
    @RequiresPermissions(value = {"business:studyFormFill:ghgsr", "business:nonTrialFormFill:ghgsr", "business:drugFormFill:ghgsr"}, logical = Logical.OR)
    public AjaxResult ghgsr(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.ghgsr(studyFormFill);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存
     */
    @PostMapping("/bc")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:studyFormFill:xz",
            "business:nonTrialFormFill:bj", "business:nonTrialFormFill:xz",
            "business:drugFormFill:bj", "business:drugFormFill:xz"}, logical = Logical.OR)
    public AjaxResult bc(@RequestBody StudyFormFill studyFormFill) {
        return AjaxResult.success(studyFormFillService.bc(studyFormFill));
    }

    /**
     * 仅保存
     */
    @PostMapping("/bcOnly")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:studyFormFill:xz",
            "business:nonTrialFormFill:bj", "business:nonTrialFormFill:xz",
            "business:drugFormFill:bj", "business:drugFormFill:xz"}, logical = Logical.OR)
    public AjaxResult bcOnly(@RequestBody StudyFormFill studyFormFill) {
        return AjaxResult.success(studyFormFillService.bcOnly(studyFormFill));
    }


    /**
     * 提交
     */
    @PostMapping("/tj")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:nonTrialFormFill:bj", "business:drugFormFill:bj"}, logical = Logical.OR)
    public AjaxResult tj(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.tj(studyFormFill);
        return AjaxResult.success("操作成功");
    }


    /**
     * 观察
     */
    @PostMapping("/gc")
    @RequiresPermissions(value = {"business:studyFormFill:gc", "business:nonTrialFormFill:gc", "business:drugFormFill:gc"}, logical = Logical.OR)
    public AjaxResult gc(@RequestBody StudyFormFillGcForm form) {
        studyFormFillService.gc(form);
        return AjaxResult.success("操作成功");
    }


    /**
     * 复核通过
     */
    @PostMapping("/fhtg")
    @RequiresPermissions(value = {"business:studyFormFill:fh", "business:nonTrialFormFill:fh", "business:drugFormFill:fh"}, logical = Logical.OR)
    public AjaxResult fhtg(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.fhtg(studyFormFill);
        return AjaxResult.success("操作成功");
    }


    /**
     * 复核拒绝
     */
    @PostMapping("/fhjj")
    @RequiresPermissions(value = {"business:studyFormFill:fh", "business:nonTrialFormFill:fh", "business:drugFormFill:fh"}, logical = Logical.OR)
    public AjaxResult fhjj(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.fhjj(studyFormFill);
        return AjaxResult.success("操作成功");
    }

    /**
     * 通过
     */
    @PostMapping("/tg")
    @RequiresPermissions(value = {"business:studyFormFill:fh", "business:nonTrialFormFill:fh", "business:drugFormFill:fh"}, logical = Logical.OR)
    public AjaxResult tg(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.tg(studyFormFill);
        return AjaxResult.success("操作成功");
    }

    /**
     * 审阅
     */
    @PostMapping("/sy")
    @RequiresPermissions(value = {"business:studyFormFill:sy", "business:nonTrialFormFill:sy", "business:drugFormFill:sy"}, logical = Logical.OR)
    public AjaxResult sy(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.sy(studyFormFill);
        return AjaxResult.success("操作成功");
    }


    /**
     * 保存复核意见
     */
    @PostMapping("/updateFhyjjl")
    public AjaxResult updateFhyjjl(@RequestBody StudyFormUpdateForm form) {
        studyFormFillService.updateFhyjjl(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存表单内容
     */
    @PostMapping("/updateBdnr")
    public AjaxResult updateBdnr(@RequestBody StudyFormUpdateForm form) {
        studyFormFillService.updateBdnr(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存字段勾选记录
     */
    @PostMapping("/updateZdgxjl")
    public AjaxResult updateZdgxjl(@RequestBody StudyFormUpdateForm form) {
        studyFormFillService.updateZdgxjl(form);
        return AjaxResult.success("操作成功");
    }


}
