package com.hxhq.business.controller;

import com.alibaba.fastjson2.JSONObject;
import com.hxhq.business.domain.Study;
import com.hxhq.business.domain.StudyJcgj;
import com.hxhq.business.dto.study.StudyListDto;
import com.hxhq.business.enums.study.StudyBorrowStatusEnum;
import com.hxhq.business.enums.study.StudyStatusEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.form.common.SignForm;
import com.hxhq.business.form.study.StudySaveForm;
import com.hxhq.business.form.study.StudySearchForm;
import com.hxhq.business.service.IStudyJcgjService;
import com.hxhq.business.service.IStudyService;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;
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.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 非试验表单Controller
 *
 * @author hxhq
 * @date 2025-12-24
 */
@RestController
@RequestMapping("/business/nonTrial")
public class StudyNonTrialController extends BaseController
{
    @Autowired
    private IStudyService studyService;
    @Autowired
    private IStudyJcgjService studyJcgjService;

    /**
     * 查询试验列表
     */
    @GetMapping("/list")
    @RequiresPermissions("business:form:nonTrial:list")
    public TableDataInfo list(StudySearchForm form)
    {
        form.setType(StudyTypeEnum.fsy.getValue());
        List<StudyListDto> list = studyService.queryNonList(form);
        TableDataInfo table = getDataTable(list);
        table.setRows(studyService.initFormCount((List<StudyListDto>)table.getRows()));
        return table;
    }

    /**
     * 获取试验详细信息
     */
    @RequiresPermissions(value = {"business:form:nonTrial:list","business:archive:nonTrial:list"},logical = Logical.OR)
    @GetMapping(value = "/info")
    public AjaxResult getInfo(Long id)
    {
        Study study = studyService.getById(id);
        if(study==null){
            return AjaxResult.error("信息不存在");
        }
        StudyListDto info = JSONObject.parseObject(JSONObject.toJSONString(study),StudyListDto.class);
        List<Long> idList = new ArrayList<>();
        idList.add(info.getId());
        List<StudyListDto> countList = studyService.queryFormCountList(idList);
        if(countList.size()>0){
            info.setFormCount(countList.get(0).getFormCount());
            info.setFormFinishCount(countList.get(0).getFormFinishCount());
        }else{
            info.setFormCount(0);
            info.setFormFinishCount(0);
        }
        return AjaxResult.success(info);
    }

    /**
     * 新增/编辑试验信息
     */
    @RequiresPermissions({"business:form:nonTrial:add", "business:form:nonTrial:edit"})
    @PostMapping("/save")
    public AjaxResult save(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        if(study==null){
            return AjaxResult.error("参数有误");
        }
        if(StringUtils.isBlank(study.getName())){
            return AjaxResult.error("试验名称不能为空");
        }
        if(StringUtils.isBlank(study.getSn())){
            return AjaxResult.error("试验编号不能为空");
        }
        if(study.getLeader()==null || study.getLeader().longValue()<=0){
            return AjaxResult.error("试验负责人不能为空");
        }
        if(study.getStatus()==null || study.getStatus().intValue()<=0){
            return AjaxResult.error("状态值有误");
        }
        if(!study.getStatus().equals(StudyStatusEnum.cg.getValue()) && !study.getStatus().equals(StudyStatusEnum.syz.getValue())){
            return AjaxResult.error("状态有误");
        }
        if(study.getId()==null){
            study.setType(StudyTypeEnum.fsy.getValue());
            study.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDept().getDeptId());
            study.setDeptName(SecurityUtils.getLoginUser().getSysUser().getDept().getDeptName());
        }
        studyService.saveInfo(form);
        return AjaxResult.success();
    }

    /**
     * 稽查轨迹列表
     * @param form
     * @return
     */
    @RequiresPermissions(value = {"business:form:nonTrial:detail","business:archive:nonTrial:xq"},logical = Logical.OR)
    @GetMapping("/jcgjList")
    public TableDataInfo jcgjList(StudyJcgj form)
    {
        startPage();
        List<StudyJcgj> list = studyJcgjService.queryList(form);
        return getDataTable(list);
    }

    /**
     * 删除试验信息
     */
    @RequiresPermissions({"business:form:nonTrial:remove"})
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.del(form);
        return AjaxResult.success();
    }

    /**
     * 检查锁定
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:sd"})
    @PostMapping("/checkSd")
    public AjaxResult checkSd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        if(study==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.checkSd(studyService.getById(study.getId()));
        return AjaxResult.success();
    }

    /**
     * 锁定
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:sd"})
    @PostMapping("/sd")
    public AjaxResult sd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.sd(form);
        return AjaxResult.success();
    }

    /**
     * 解锁
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:js"})
    @PostMapping("/js")
    public AjaxResult js(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.js(form);
        return AjaxResult.success();
    }

    /**
     * 归档
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:gd"})
    @PostMapping("/gd")
    public AjaxResult gd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.gd(form);
        return AjaxResult.success();
    }

    /**
     * 解档
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:jd"})
    @PostMapping("/jd")
    public AjaxResult jd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.jd(form);
        return AjaxResult.success();
    }

    /**
     * 借阅
     * @param form
     * @return
     */
    @RequiresPermissions({"business:form:nonTrial:jy"})
    @PostMapping("/jy")
    public AjaxResult jy(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0 || StringUtils.isBlank(sign.getStartDate()) || StringUtils.isBlank(sign.getEndDate())){
            return AjaxResult.error("参数有误");
        }
        studyService.jy(form);
        return AjaxResult.success();
    }

    // region 档案相关

    /**
     * 查询档案列表
     */
    @GetMapping("/archiveList")
    @RequiresPermissions("business:archive:nonTrial:list")
    public TableDataInfo archiveList(StudySearchForm form)
    {
        form.setType(StudyTypeEnum.fsy.getValue());
        startPage();
        List<StudyListDto> list = studyService.queryArchiveList(form);
        TableDataInfo table = getDataTable(list);
        return table;
    }

    /**
     * 同意/拒绝归档
     * @param form
     * @return
     */
    @RequiresPermissions(value = {"business:archive:nonTrial:tygd","business:archive:nonTrial:jjgd"},logical = Logical.OR)
    @PostMapping("/doGd")
    public AjaxResult doGd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0 || study.getStatus()==null){
            return AjaxResult.error("参数有误");
        }
        if(!study.getStatus().equals(StudyStatusEnum.ysd.getValue()) && !study.getStatus().equals(StudyStatusEnum.gd.getValue())){
            return AjaxResult.error("状态有误");
        }
        if(study.getStatus().equals(StudyStatusEnum.ysd.getValue()) && StringUtils.isBlank(sign.getRemark())){
            return AjaxResult.error("原因不能为空");
        }
        studyService.doGd(form);
        return AjaxResult.success();
    }

    /**
     * 同意/拒绝解档
     * @param form
     * @return
     */
    @RequiresPermissions(value = {"business:archive:nonTrial:tyjd","business:archive:nonTrial:jjjd"},logical = Logical.OR)
    @PostMapping("/doJd")
    public AjaxResult doJd(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0 || study.getStatus()==null){
            return AjaxResult.error("参数有误");
        }
        if(!study.getStatus().equals(StudyStatusEnum.ysd.getValue()) && !study.getStatus().equals(StudyStatusEnum.gd.getValue())){
            return AjaxResult.error("状态有误");
        }
        if(study.getStatus().equals(StudyStatusEnum.gd.getValue()) && StringUtils.isBlank(sign.getRemark())){
            return AjaxResult.error("原因不能为空");
        }
        studyService.doJd(form);
        return AjaxResult.success();
    }

    /**
     * 同意/拒绝借阅
     * @param form
     * @return
     */
    @RequiresPermissions(value = {"business:archive:nonTrial:tyjy","business:archive:nonTrial:jjjy"},logical = Logical.OR)
    @PostMapping("/doJy")
    public AjaxResult doJy(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0 || study.getBorrowStatus()==null){
            return AjaxResult.error("参数有误");
        }
        if(!study.getBorrowStatus().equals(StudyBorrowStatusEnum.jyz.getValue()) && !study.getBorrowStatus().equals(StudyBorrowStatusEnum.wjy.getValue())){
            return AjaxResult.error("状态有误");
        }
        if(study.getBorrowStatus().equals(StudyBorrowStatusEnum.wjy.getValue()) && StringUtils.isBlank(sign.getRemark())){
            return AjaxResult.error("原因不能为空");
        }
        studyService.doJy(form);
        return AjaxResult.success();
    }

    /**
     * 同意/拒绝借阅
     * @param form
     * @return
     */
    @RequiresPermissions("business:archive:nonTrial:qrgh")
    @PostMapping("/gh")
    public AjaxResult gh(@RequestBody StudySaveForm form)
    {
        Study study = form.getStudy();
        SignForm sign = form.getSign();
        if(study==null || sign==null){
            return AjaxResult.error("参数有误");
        }
        if(study.getId()==null || study.getId().longValue()<=0){
            return AjaxResult.error("参数有误");
        }
        studyService.gh(form);
        return AjaxResult.success();
    }

    //endregion
}
