package com.hxhq.business.controller;

import java.util.List;

import com.hxhq.business.domain.*;
import com.hxhq.business.dto.study.StudyFormApplyListDto;
import com.hxhq.business.form.study.StudyFormApplyGcForm;
import com.hxhq.business.form.study.StudyFormApplyShfzForm;
import com.hxhq.business.form.study.StudyFormApplySearchForm;
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/studyFormApply")
public class StudyFormApplyController extends BaseController {
    @Autowired
    private IStudyFormApplyService studyFormApplyService;
    @Autowired
    private IStudyFormApplyJcgjService studyFormApplyJcgjService;
    @Autowired
    private IStudyFormApplyQmxxService studyFormApplyQmxxService;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:drugFormApply:list")
    public TableDataInfo list(StudyFormApplySearchForm form) {
        startPage();
        List<StudyFormApplyListDto> list = studyFormApplyService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @RequiresPermissions({"business:drugFormApply:xq"})
    @GetMapping(value = "/exportDetail")
    public AjaxResult exportDetail(StudyFormApplyJcgj form) {
        List<StudyFormApplyJcgj> studyFormApplyJcgjList = studyFormApplyJcgjService.queryList(form);
        StudyFormApplyQmxx studyFormApplyQmxx = new StudyFormApplyQmxx();
        studyFormApplyQmxx.setFormId(form.getFormId());
        List<StudyFormApplyQmxx> studyFormApplyQmxxList = studyFormApplyQmxxService.queryList(studyFormApplyQmxx);
        StudyFormApply studyFormApply=studyFormApplyService.queryInfo(form.getFormId());
        return AjaxResult.success(localFilePrefix + PdfExportUtil.export(studyFormApply.getTemplateExportClass(), studyFormApply.getTemplateExportMethod(),studyFormApply,studyFormApplyQmxxList,studyFormApplyJcgjList,form.getLang(),form.getJcgjlx(),localFilePath));
    }

    /**
     * 稽查轨迹列表
     */
    @GetMapping("/jcgjList")
    @RequiresPermissions("business:drugFormApply:xq")
    public TableDataInfo jcgjList(StudyFormApplyJcgj form) {
        startPage();
        List<StudyFormApplyJcgj> list = studyFormApplyJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 签名信息列表
     */
    @GetMapping("/qmxxList")
    @RequiresPermissions("business:drugFormApply:xq")
    public TableDataInfo qmxxList(StudyFormApplyQmxx form) {
        startPage();
        List<StudyFormApplyQmxx> list = studyFormApplyQmxxService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 详细
     */
    @GetMapping(value = "/info")
    @RequiresPermissions("business:drugFormApply:xq")
    public AjaxResult getInfo(Long id) {
        return AjaxResult.success(studyFormApplyService.queryInfo(id));
    }

    /**
     * 加签
     */
    @RequiresPermissions("business:drugFormApply:jq")
    @PostMapping("/jq")
    public AjaxResult jq(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.jq(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 废止
     */
    @RequiresPermissions("business:drugFormApply:fz")
    @PostMapping("/fz")
    public AjaxResult fz(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.fz(studyFormApply);
        return AjaxResult.success("操作成功");
    }


    /**
     * 确认废止
     */
    @RequiresPermissions("business:drugFormApply:qrfz")
    @PostMapping("/qrfz")
    public AjaxResult qrfz(@RequestBody StudyFormApplyShfzForm form) {
        studyFormApplyService.qrfz(form);
        return AjaxResult.success("操作成功");
    }


    /**
     * 更换归属人
     */
    @RequiresPermissions("business:drugFormApply:ghgsr")
    @PostMapping("/ghgsr")
    public AjaxResult ghgsr(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.ghgsr(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 保存
     */
    @PostMapping("/bc")
    @RequiresPermissions(value={"business:drugFormApply:bj","business:drugFormApply:xz"}, logical= Logical.OR)
    public AjaxResult bc(@RequestBody StudyFormApply studyFormApply) {
        return AjaxResult.success(studyFormApplyService.bc(studyFormApply));
    }

    /**
     * 仅保存
     */
    @PostMapping("/bcOnly")
    @RequiresPermissions(value={"business:drugFormApply:bj","business:drugFormApply:xz"}, logical= Logical.OR)
    public AjaxResult bcOnly(@RequestBody StudyFormApply studyFormApply) {
        return AjaxResult.success(studyFormApplyService.bcOnly(studyFormApply));
    }



    /**
     * 提交
     */
    @RequiresPermissions("business:drugFormApply:bj")
    @PostMapping("/tj")
    public AjaxResult tj(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.tj(studyFormApply);
        return AjaxResult.success("操作成功");
    }


    /**
     * 复核通过
     */
    @RequiresPermissions("business:drugFormApply:fh")
    @PostMapping("/fhtg")
    public AjaxResult fhtg(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.fhtg(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 复核拒绝
     */
    @RequiresPermissions("business:drugFormApply:fh")
    @PostMapping("/fhjj")
    public AjaxResult fhjj(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.fhjj(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 审阅
     */
    @RequiresPermissions("business:drugFormApply:sy")
    @PostMapping("/sy")
    public AjaxResult sy(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.sy(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 审核通过
     */
    @RequiresPermissions("business:drugFormApply:sh")
    @PostMapping("/shtg")
    public AjaxResult shtg(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.shtg(studyFormApply);
        return AjaxResult.success("操作成功");
    }

    /**
     * 审核拒绝
     */
    @RequiresPermissions("business:drugFormApply:sh")
    @PostMapping("/shjj")
    public AjaxResult shjj(@RequestBody StudyFormApply studyFormApply) {
        studyFormApplyService.shjj(studyFormApply);
        return AjaxResult.success("操作成功");
    }

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

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

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

}
