package com.hxhq.business.controller;

import java.io.File;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.*;
import com.hxhq.business.dto.study.StudyFormFillListDto;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.enums.zykgl.JyztEnum;
import com.hxhq.business.enums.zykgl.ZjztEnum;
import com.hxhq.business.form.sj.SjBjForm;
import com.hxhq.business.form.sj.SjSubpackageForm;
import com.hxhq.business.form.study.*;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.StudyFormFillUtil;
import com.hxhq.business.utils.pdf.PdfExportUtil;
import com.hxhq.business.utils.pdf.template.TemplateBaseUtil;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.security.annotation.Logical;
import com.hxhq.common.security.annotation.RequiresPermissions;
import com.hxhq.common.security.utils.SecurityUtils;
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 IStudyService studyService;
    @Autowired
    private IStudyFormFillJcgjService studyFormFillJcgjService;
    @Autowired
    private IStudyFormFillQmxxService studyFormFillQmxxService;
    @Autowired
    private ITemplateService templateService;
    /**
     * 上传文件存储在本地的根路径
     */
    @Value("${file.path}")
    private String localFilePath;
    /**
     * 资源映射路径 前缀
     */
    @Value("${file.prefix}")
    public String localFilePrefix;

    /**
     * 开始配置
     *
     * @return
     */
    @PostMapping("/startConfiguration")
    @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 startConfiguration(@RequestBody StudyFormConfigurationForm form) {
        studyFormFillService.startConfiguration(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 配置完成
     *
     * @param form
     * @return
     */
    @PostMapping("/configurationCompleted")
    @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 configurationCompleted(@RequestBody StudyFormConfigurationForm form) {
        studyFormFillService.configurationCompleted(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 分装
     *
     * @param form
     * @return
     */
    @PostMapping("/subpackage")
    @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 subpackage(@RequestBody StudyFormFillSubpackageForm form) {
        studyFormFillService.subpackage(form);
        return AjaxResult.success("操作成功");
    }

    /**
     * 列表
     */
    @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("/jcgjqmxxList")
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    public AjaxResult jcgjqmxxList(Integer jcgjlx, Long id) {
        HashMap<String, Object> map = new HashMap<String, Object>(2);
        //稽查轨迹
        QueryWrapper<StudyFormFillJcgj> studyFormFillJcgjQueryWrapper = Wrappers.query();
        studyFormFillJcgjQueryWrapper.eq("form_id", id);
        if (jcgjlx != null && jcgjlx.intValue() > 0) {
            Integer jcgjlxExport = 999;
            if (jcgjlx.intValue() == jcgjlxExport) {
                studyFormFillJcgjQueryWrapper.notIn("jcgjlx", JcgjlxEnum.xg.getValue());
            } else {
                studyFormFillJcgjQueryWrapper.eq("jcgjlx", jcgjlx);
            }
        }
        studyFormFillJcgjQueryWrapper.orderByDesc("create_time");
        map.put("jcgj", studyFormFillJcgjService.list(studyFormFillJcgjQueryWrapper));
        //签名信息
        QueryWrapper<StudyFormFillQmxx> studyFormFillQmxxQueryWrapper = Wrappers.query();
        studyFormFillQmxxQueryWrapper.eq("form_id", id);
        studyFormFillQmxxQueryWrapper.orderByDesc("create_time");
        map.put("qmxx", studyFormFillQmxxService.list(studyFormFillQmxxQueryWrapper));
        return AjaxResult.success(map);
    }

    /**
     * 详细
     */
    @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));
    }


    /**
     * 导出
     */
    @RequiresPermissions(value = {"business:studyFormFill:xq", "business:nonTrialFormFill:xq", "business:drugFormFill:xq"}, logical = Logical.OR)
    @GetMapping(value = "/exportByFileUrl")
    public AjaxResult exportByFileUrl(StudyFormFillExportForm form) {
        StudyFormFill studyFormFill = studyFormFillService.queryInfo(form.getStudyFormFillId());
        Study study = studyService.getById(studyFormFill.getStudyId());
        //稽查轨迹
        List<StudyFormFillJcgj> jcgjList = new ArrayList<>();
        Integer noExport=999;
        Boolean jcgj=false;
        if (form.getJcgjlx() != null && form.getJcgjlx().intValue() > 0&&!form.getJcgjlx().equals(noExport)) {
            QueryWrapper<StudyFormFillJcgj> studyFormFillJcgjQueryWrapper = Wrappers.query();
            studyFormFillJcgjQueryWrapper.eq("form_id", studyFormFill.getId());
            studyFormFillJcgjQueryWrapper.eq("jcgjlx", form.getJcgjlx());
            studyFormFillJcgjQueryWrapper.orderByDesc("create_time");
            jcgjList = studyFormFillJcgjService.list(studyFormFillJcgjQueryWrapper);
            jcgj=true;
        }

        //签名信息
        QueryWrapper<StudyFormFillQmxx> studyFormFillQmxxQueryWrapper = Wrappers.query();
        studyFormFillQmxxQueryWrapper.eq("form_id", studyFormFill.getId());
        studyFormFillQmxxQueryWrapper.orderByDesc("create_time");
        List<StudyFormFillQmxx> qmxxList = studyFormFillQmxxService.list(studyFormFillQmxxQueryWrapper);

        String fileUrl = form.getUrl();
        fileUrl = fileUrl.replaceFirst(localFilePrefix, "");
        int indexOf = fileUrl.lastIndexOf("/");
        String path = fileUrl.substring(0, indexOf);
        String exportFileName = StudyFormFillUtil.export(form.getVersion(), form.getLang(), study.getName() + "(" + study.getSn() + ")   " + studyFormFill.getBdmc(),
                localFilePath + fileUrl, localFilePath + File.separator + path, jcgjList, qmxxList,jcgj);


        return AjaxResult.success(localFilePrefix + path + File.separator + exportFileName);
    }

    /**
     * 加签
     */
    @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("/tjblxjsh")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:nonTrialFormFill:bj", "business:drugFormFill:bj"}, logical = Logical.OR)
    public AjaxResult tjblxjsh(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.tjblxjsh(studyFormFill);
        return AjaxResult.success("操作成功");
    }

    /**
     * 药剂存储过滤
     */
    @PostMapping("/yjccFilter")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:nonTrialFormFill:bj", "business:drugFormFill:bj"}, logical = Logical.OR)
    public AjaxResult yjccFilter(@RequestBody StudyFormFill studyFormFill) {
        return AjaxResult.success(studyFormFillService.yjccFilter(studyFormFill));
    }

    /**
     * 药剂存储
     */
    @PostMapping("/yjcc")
    @RequiresPermissions(value = {"business:studyFormFill:bj", "business:nonTrialFormFill:bj", "business:drugFormFill:bj"}, logical = Logical.OR)
    public AjaxResult yjcc(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.yjcc(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("/blxjshtg")
    @RequiresPermissions(value = {"business:studyFormFill:blxjsh", "business:nonTrialFormFill:blxjsh", "business:drugFormFill:blxjsh"}, logical = Logical.OR)
    public AjaxResult blxjshtg(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.blxjshtg(studyFormFill);
        return AjaxResult.success("操作成功");
    }


    /**
     * 病理学家审核拒绝
     */
    @PostMapping("/blxjshjj")
    @RequiresPermissions(value = {"business:studyFormFill:blxjsh", "business:nonTrialFormFill:blxjsh", "business:drugFormFill:blxjsh"}, logical = Logical.OR)
    public AjaxResult blxjshjj(@RequestBody StudyFormFill studyFormFill) {
        studyFormFillService.blxjshjj(studyFormFill);
        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("操作成功");
    }


}
