package com.hxhq.business.service.impl;

import java.util.*;

import com.alibaba.fastjson2.JSONObject;
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.NormalEnum;
import com.hxhq.business.enums.SnTypeEnum;
import com.hxhq.business.enums.study.StudyFormFillBdztEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.enums.template.ProductEnum;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.study.*;
import com.hxhq.business.form.study.StudyFormFillSearchForm;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.business.utils.lang.StudyFormUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysDept;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysDeptService;
import com.hxhq.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.StudyFormFillMapper;
import com.hxhq.business.domain.StudyFormFill;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-填报单Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-29
 */
@Service
public class StudyFormFillServiceImpl extends ServiceImpl<StudyFormFillMapper, StudyFormFill> implements IStudyFormFillService {

    @Autowired
    private IStudyFormFillJcgjService studyFormFillJcgjService;
    @Autowired
    private IStudyFormFillQmxxService studyFormFillQmxxService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ITemplateService templateService;
    @Autowired
    private ISjService sjService;
    @Autowired
    private ISnGenService snGenService;
    @Autowired
    private IStudyService studyService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IMjyService mjyService;
    @Autowired
    private IGyzjService gyzjService;

    /**
     * 查询试验-填报单列表
     *
     * @param form 试验-填报单
     * @return 试验-填报单
     */
    @Override
    public List<StudyFormFillListDto> queryList(StudyFormFillSearchForm form) {
        QueryWrapper<StudyFormFill> queryWrapper = Wrappers.query();
        queryWrapper.eq("t.del_flag", "0");
        if (form.getUserId() != null && form.getUserId().longValue() > 0) {
            queryWrapper.eq("t.user_id", form.getUserId());
        }
        if (form.getStudyId() != null && form.getStudyId().longValue() > 0) {
            queryWrapper.eq("t.study_id", form.getStudyId());
        }
        if (form.getStudySubjectId() != null && form.getStudySubjectId().longValue() > 0) {
            queryWrapper.eq("t.study_subject_id", form.getStudySubjectId());
        }
        if (StringUtils.isNoneBlank(form.getSfbl())) {
            queryWrapper.eq("t.sfbl", form.getSfbl());
        }
        if (StringUtils.isNoneBlank(form.getBdbh())) {
            queryWrapper.and(p -> p.like("t.`bdbh`", form.getBdbh()));
        }
        if (StringUtils.isNoneBlank(form.getBdmc())) {
            queryWrapper.and(p -> p.like("t.`bdmc`", form.getBdmc()));
        }
        if (StringUtils.isNoneBlank(form.getTemplateMc())) {
            queryWrapper.and(p -> p.like("tm.`name`", form.getTemplateMc()));
        }
        if (StringUtils.isNoneBlank(form.getTjsjks())) {
            queryWrapper.apply("t.tjsj>={0}", form.getTjsjks());
        }
        if (StringUtils.isNoneBlank(form.getTjsjjs())) {
            queryWrapper.apply("t.tjsj<{0}", form.getTjsjjs());
        }
        if (StringUtils.isNoneBlank(form.getCjsjks())) {
            queryWrapper.apply("t.create_time>={0}", form.getCjsjks());
        }
        if (StringUtils.isNoneBlank(form.getCjsjjs())) {
            queryWrapper.apply("t.create_time<{0}", form.getCjsjjs());
        }
        queryWrapper.orderByDesc("t.create_time");
        return baseMapper.queryList(queryWrapper);
    }

    /**
     * 详情
     *
     * @param id
     * @return 详情
     */
    @Override
    public StudyFormFill queryInfo(Long id) {
        if (id == null || id.longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFill = baseMapper.queryInfo(id);
        return studyFormFill;
    }

    /**
     * 加签 ok
     *
     * @param studyFormFill
     */
    @Override
    public void jq(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.tbz.getValue())) {
            throw new ServiceException("表单不是填报状态，不能加签");
        }
        //稽查轨迹
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.bj.getValue(), studyFormFill.getQmyy(), JcmcysEnum.orange.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), studyFormFill.getQmyy(), qmr, studyFormFill.getRemark());

    }

    /**
     * 废止
     *
     * @param studyFormFill
     */
    @Override
    public void fz(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.tbz.getValue())) {
            throw new ServiceException("表单不是填报状态，不能废止");
        }
        studyFormFillOld.setBdzt(StudyFormFillBdztEnum.dfz.getValue());
        this.updateById(studyFormFillOld);
        //稽查轨迹
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "申请废止", JcmcysEnum.orange.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), null, null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), "申请废止", qmr, studyFormFill.getRemark());

    }


    /**
     * 审核废止
     *
     * @param form
     */
    @Override
    public void qrfz(StudyFormFillShfzForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm());
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(form.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.dfz.getValue())) {
            throw new ServiceException("表单不是填报状态，不能废止");
        }
        if (form.getQmyy().equals(NormalEnum.yes.getValue())) {
            studyFormFillOld.setBdzt(StudyFormFillBdztEnum.yfz.getValue());
            this.updateById(studyFormFillOld);
            //稽查轨迹
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("原因", form.getRemark());
            studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "废止通过", JcmcysEnum.green.getValue(), formData, SecurityUtils.getLoginUser().getSysUser(), null, null);
            //签名信息
            studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "废止通过", qmr, form.getRemark());
            //发送通知
            Study study = studyService.getById(studyFormFillOld.getStudyId());
            String url = getUrlQz(study,"tbbd");
            String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】废止通过";
            noticeService.save(title,studyFormFillOld.getUserId(),url);
        } else {
            studyFormFillOld.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
            this.updateById(studyFormFillOld);
            //稽查轨迹
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("原因", form.getRemark());
            studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "废止拒绝", JcmcysEnum.red.getValue(), formData, SecurityUtils.getLoginUser().getSysUser(), null, null);
            //签名信息
            studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "废止拒绝", qmr, form.getRemark());
            //发送通知
            Study study = studyService.getById(studyFormFillOld.getStudyId());
            String url = getUrlQz(study,"tbbd");
            String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】废止拒绝";
            noticeService.save(title,studyFormFillOld.getUserId(),url);
        }

    }


    /**
     * 更换归属人
     *
     * @param studyFormFill
     */
    @Override
    public void ghgsr(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.dfz.getValue()) || studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.yfz.getValue())) {
            throw new ServiceException("表单已废止或待废止，不能更换归属人");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("新归属人", studyFormFill.getUserMc());
        formData.put("原归属人", studyFormFillOld.getUserMc());
        formData.put("原因", studyFormFill.getRemark());

        //是否验证新的归属人，是否属于该实验？ todo
        studyFormFillOld.setUserId(studyFormFill.getUserId());
        studyFormFillOld.setUserMc(studyFormFill.getUserMc());
        this.updateById(studyFormFillOld);
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "更换归属人", JcmcysEnum.orange.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), null, null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "更换归属人", qmr, studyFormFill.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormFillOld.getStudyId());
        String url = getUrlQz(study,"tbbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】已更换您为归属人";
        noticeService.save(title,studyFormFillOld.getUserId(),url);
    }

    /**
     * 保存
     *
     * @param studyFormFill
     */
    @Override
    public StudyFormFill bc(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getQmrmm());
        StudyFormFill result = null;
        if (studyFormFill.getId() != null) {
            StudyFormFill studyFormFillOld = this.queryInfo(studyFormFill.getId());
            if (studyFormFillOld == null) {
                throw new ServiceException("信息不存在或已删除");
            }
            if (!SecurityUtils.getUserId().equals(studyFormFillOld.getUserId())) {
                throw new ServiceException("只能操作自己的表单");
            }
            studyFormFillOld.setBdmc(studyFormFill.getBdmc());
            studyFormFillOld.setBdnr(studyFormFill.getBdnr());
            studyFormFillOld.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
            this.updateById(studyFormFillOld);
            result = studyFormFillOld;
            //签名信息
            studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), "填写并保存记录", qmr, studyFormFill.getRemark());
            //稽查轨迹
            studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "填写并保存记录", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        } else {
            studyFormFill.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
            studyFormFill.setUserId(SecurityUtils.getUserId());
            studyFormFill.setBdbh(getSn(studyFormFill));
            studyFormFill.setStartDate(new Date());
            studyFormFill.setUserMc(SecurityUtils.getNickName());
            this.save(studyFormFill);
            result = studyFormFill;
            //签名信息
            studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), "填写并保存记录", qmr, studyFormFill.getRemark());
            //稽查轨迹
            studyFormFillJcgjService.saveJcgj(studyFormFill.getId(), JcgjlxEnum.lc.getValue(), "创建记录", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        }
        return baseMapper.queryInfo(result.getId());
    }

    /**
     * 保存
     *
     * @param studyFormFill
     */
    @Override
    public void bcAuto(StudyFormFill studyFormFill) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        studyFormFill.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
        studyFormFill.setUserId(SecurityUtils.getUserId());
        studyFormFill.setBdbh(getSn(studyFormFill));
        studyFormFill.setStartDate(new Date());
        studyFormFill.setUserMc(SecurityUtils.getNickName());
        this.save(studyFormFill);
        //稽查轨迹
        studyFormFillJcgjService.saveJcgj(studyFormFill.getId(), JcgjlxEnum.lc.getValue(), "创建记录", JcmcysEnum.green.getValue(), null,qmr , studyFormFill.getRemark(), null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), "创建记录", qmr, studyFormFill.getRemark());
    }

    /**
     * 仅保存
     *
     * @param studyFormFill
     */
    @Override
    public StudyFormFill bcOnly(StudyFormFill studyFormFill) {
        StudyFormFill result = null;
        if (studyFormFill.getId() != null) {
            StudyFormFill studyFormFillOld = this.queryInfo(studyFormFill.getId());
            if (studyFormFillOld == null) {
                throw new ServiceException("信息不存在或已删除");
            }
            if (!SecurityUtils.getUserId().equals(studyFormFillOld.getUserId())) {
                throw new ServiceException("只能操作自己的表单");
            }
            studyFormFillOld.setBdmc(studyFormFill.getBdmc());
            studyFormFillOld.setBdnr(studyFormFill.getBdnr());
            studyFormFillOld.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
            this.updateById(studyFormFillOld);
            result = studyFormFillOld;
        } else {
            studyFormFill.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
            studyFormFill.setUserId(SecurityUtils.getUserId());
            studyFormFill.setBdbh(getSn(studyFormFill));
            studyFormFill.setStartDate(new Date());
            studyFormFill.setUserMc(SecurityUtils.getNickName());
            this.save(studyFormFill);
            result = studyFormFill;
        }
        return baseMapper.queryInfo(result.getId());
    }

    /**
     * 提交
     *
     * @param studyFormFill
     */
    @Override
    public void tj(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getQmrmm());
        if (studyFormFill.getId() != null) {
            StudyFormFill studyFormFillOld = this.queryInfo(studyFormFill.getId());
            studyFormFillOld.setBdmc(studyFormFill.getBdmc());
            studyFormFillOld.setBdnr(studyFormFill.getBdnr());
            studyFormFillOld.setResource(studyFormFill.getResource());
            studyFormFillOld.setBdzt(StudyFormFillBdztEnum.ytj.getValue());
            studyFormFillOld.setTjsj(new Date());
            if (studyFormFillOld.getSftb().equals(NormalEnum.no.getValue())) {
                studyFormFillOld.setEndDate(new Date());
                studyFormFillOld.setCclist(studyFormFill.getCclist());
                studyFormFillOld.setCzlist(studyFormFill.getCzlist());
                //生成物资
                genResource(studyFormFillOld,studyFormFill.getRemark());
            }
            studyFormFillOld.setSftb(NormalEnum.yes.getValue());
            this.updateById(studyFormFillOld);
            List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(studyFormFillOld, studyFormFill);
            if (fieldChanges.size() > 0) {
                //稽查轨迹
                List<StudyFormFillJcgj> studyFormFillJcgjs = new ArrayList<>();
                for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
                    studyFormFillJcgjs.add(studyFormFillJcgjService.getJcgj(studyFormFillOld.getId(), JcgjlxEnum.xg.getValue(), "修改", JcmcysEnum.orange.getValue(), fieldChange.toString(), fieldChange.toEnString(), SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark()));
                }
                studyFormFillJcgjService.saveBatch(studyFormFillJcgjs);
            }
            //稽查轨迹
            studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "填写并提交记录", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        } else {
            studyFormFill.setBdzt(StudyFormFillBdztEnum.ytj.getValue());
            studyFormFill.setUserId(SecurityUtils.getUserId());
            studyFormFill.setUserMc(SecurityUtils.getNickName());
            studyFormFill.setSftb(NormalEnum.yes.getValue());
            studyFormFill.setBdbh(getSn(studyFormFill));
            studyFormFill.setTjsj(new Date());
            this.save(studyFormFill);
            //生成物资
            genResource(studyFormFill,studyFormFill.getRemark());
            //稽查轨迹
            studyFormFillJcgjService.saveJcgj(studyFormFill.getId(), JcgjlxEnum.lc.getValue(), "填写并提交记录", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        }
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFill.getId(), "填写并提交记录", qmr, studyFormFill.getRemark());

    }

    /**
     * 生成物资
     *
     * @param studyFormFill
     * @param remark
     */
    public void genResource(StudyFormFill studyFormFill,String remark) {
        Template template = templateService.getById(studyFormFill.getTemplateId());
        // 处置，存放，用量
        if (template.getProduct().equals(ProductEnum.Sj.getValue())) {
            sjService.genResource(studyFormFill, template);
        }
        if (template.getProduct().equals(ProductEnum.Gyzj.getValue())) {
            gyzjService.genResource(studyFormFill, template,remark);
        }
        if (template.getProduct().equals(ProductEnum.Mjy.getValue())) {
            mjyService.genResource(studyFormFill, template,remark);
        }
    }

    /**
     * 观察
     *
     * @param form
     */
    @Override
    public void gc(StudyFormFillGcForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm());
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(form.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.tbz.getValue())) {
            throw new ServiceException("表单不是填报状态，不能观察");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("生长情况", form.getQmyy());
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), form.getQmyy(), JcmcysEnum.orange.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), form.getRemark(), null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), form.getQmyy(), qmr, form.getRemark());

    }


    /**
     * 复核通过
     *
     * @param studyFormFill
     */
    @Override
    public void fhtg(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.ytj.getValue())) {
            throw new ServiceException("表单不是已提交状态，不能复核");
        }
        studyFormFillOld.setBdzt(StudyFormFillBdztEnum.ywcfh.getValue());
        studyFormFillOld.setBdnr(studyFormFill.getBdnr());
        this.updateById(studyFormFillOld);
        //稽查轨迹
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "复核通过", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "复核通过", qmr, studyFormFill.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormFillOld.getStudyId());
        String url = getUrlQz(study,"tbbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】复核通过";
        noticeService.save(title,studyFormFillOld.getUserId(),url);
    }


    /**
     * 复核拒绝
     *
     * @param studyFormFill
     */
    @Override
    public void fhjj(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.ytj.getValue())) {
            throw new ServiceException("表单不是已提交状态，不能复核");
        }
        studyFormFillOld.setBdzt(StudyFormFillBdztEnum.tbz.getValue());
        studyFormFillOld.setBdnr(studyFormFill.getBdnr());
        this.updateById(studyFormFillOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", studyFormFill.getRemark());
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "复核拒绝", JcmcysEnum.red.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), null, null);

        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "复核拒绝", qmr, studyFormFill.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormFillOld.getStudyId());
        String url = getUrlQz(study,"tbbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】复核拒绝";
        noticeService.save(title,studyFormFillOld.getUserId(),url);

    }

    /**
     * 通过
     *
     * @param studyFormFill
     */
    @Override
    public void tg(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.ytj.getValue())) {
            throw new ServiceException("表单不是已提交状态，不能复核");
        }
        studyFormFillOld.setBdzt(StudyFormFillBdztEnum.ywc.getValue());
        studyFormFillOld.setBdnr(studyFormFill.getBdnr());
        this.updateById(studyFormFillOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", studyFormFill.getRemark());
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "免复核通过", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), null, null);

        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "免复核通过", qmr, studyFormFill.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormFillOld.getStudyId());
        String url = getUrlQz(study,"tbbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】免复核通过";
        noticeService.save(title,studyFormFillOld.getUserId(),url);
    }

    /**
     * 审阅
     *
     * @param studyFormFill
     */
    @Override
    public void sy(StudyFormFill studyFormFill) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormFill.getQmrmm());
        if (studyFormFill.getId() == null || studyFormFill.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormFill studyFormFillOld = this.getById(studyFormFill.getId());
        if (studyFormFillOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!(studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.ywcfh.getValue()) || studyFormFillOld.getBdzt().equals(StudyFormFillBdztEnum.ywc.getValue()))) {
            throw new ServiceException("表单不是已完成状态，不能审阅");
        }
        studyFormFillOld.setBdnr(studyFormFill.getBdnr());
        this.updateById(studyFormFillOld);
        //稽查轨迹
        studyFormFillJcgjService.saveJcgj(studyFormFillOld.getId(), JcgjlxEnum.lc.getValue(), "已审阅", JcmcysEnum.green.getValue(), null, SecurityUtils.getLoginUser().getSysUser(), studyFormFill.getRemark(), null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormFillOld.getId(), "已审阅", qmr, studyFormFill.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormFillOld.getStudyId());
        String url = getUrlQz(study,"tbbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下填报表单【"+studyFormFillOld.getBdmc()+"】已审阅";
        noticeService.save(title,studyFormFillOld.getUserId(),url);
    }

    /**
     * 验证密码
     *
     * @param user
     * @param qmrmm
     */
    public void checkPassword(SysUser user, String qmrmm) {
        if (StringUtils.isBlank(qmrmm)) {
            throw new ServiceException("签名密码不能为空");
        }
        sysUserService.checkPassword(user, qmrmm, false);
    }

    /**
     * 更新表单内容
     */
    @Override
    public void updateBdnr(StudyFormUpdateForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        baseMapper.updateBdnr(form.getId(), form.getBdnr(),form.getZdxgjl());
        //稽查轨迹
        if (StringUtils.isNoneBlank(form.getFiledValue())) {
            JSONObject jsonObject = JSONObject.parseObject(form.getFiledValue());
            Map<String, String> formData = new LinkedHashMap<>();
            Map<String, String> formDataEn = new LinkedHashMap<>();
            List<StudyFormFillJcgj> studyFormFillJcgjs = new ArrayList<>();
            if (StringUtils.isNoneBlank(jsonObject.getString("oldValue"))) {
                //zh
                formData.put("字段名", jsonObject.getString("fieldCn"));
                formData.put("原值", jsonObject.getString("oldValue"));
                formData.put("新值", jsonObject.getString("value"));
                formData.put("原因", jsonObject.getString("reason"));
                //en
                formDataEn.put("字段名", jsonObject.getString("fieldEn"));
                formDataEn.put("原值", jsonObject.getString("oldValue"));
                formDataEn.put("新值", jsonObject.getString("value"));
                formDataEn.put("原因", jsonObject.getString("reason"));
                studyFormFillJcgjs.add(studyFormFillJcgjService.getJcgj(form.getId(), JcgjlxEnum.xg.getValue(), "修改记录", JcmcysEnum.orange.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), qmr, null));
            } else {
                //zh
                formData.put("字段名", jsonObject.getString("fieldCn"));
                formData.put("填入值", jsonObject.getString("value"));
                formData.put("填写人", qmr.getNickName());
                //en
                formDataEn.put("字段名", jsonObject.getString("fieldEn"));
                formDataEn.put("填入值", jsonObject.getString("value"));
                formDataEn.put("填写人", qmr.getNickName());
                studyFormFillJcgjs.add(studyFormFillJcgjService.getJcgj(form.getId(), JcgjlxEnum.xg.getValue(), "填写", JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), null, null));
            }
            studyFormFillJcgjService.saveBatch(studyFormFillJcgjs);
        }
    }

    /**
     * 更新复核意见
     */
    @Override
    public void updateFhyjjl(StudyFormUpdateForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //更新复核意见
        baseMapper.updateFhyjjl(form.getId(), form.getFhyjjl());
        Map<String, String> formData = new LinkedHashMap<>();
        Map<String, String> formDataEn = new LinkedHashMap<>();
        List<StudyFormFillJcgj> studyFormFillJcgjs = new ArrayList<>();
        if (StringUtils.isNoneBlank(form.getContent())) {
            JSONObject jsonObject = JSONObject.parseObject(form.getContent());
            //zh
            formData.put("字段名", jsonObject.getString("fieldCn"));
            formData.put("复核意见", jsonObject.getString("content"));
            //en
            formDataEn.put("字段名", jsonObject.getString("fieldEn"));
            formDataEn.put("复核意见", jsonObject.getString("content"));
            studyFormFillJcgjs.add(studyFormFillJcgjService.getJcgj(form.getId(), JcgjlxEnum.lc.getValue(), "复核意见", JcmcysEnum.blue.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), qmr, null));
        } else {
            JSONObject jsonObject = JSONObject.parseObject(form.getReply());
            //zh
            formData.put("字段名", jsonObject.getString("fieldCn"));
            formData.put("意见回复", jsonObject.getString("reply"));
            //en
            formDataEn.put("字段名", jsonObject.getString("fieldEn"));
            formDataEn.put("意见回复", jsonObject.getString("reply"));
            studyFormFillJcgjs.add(studyFormFillJcgjService.getJcgj(form.getId(), JcgjlxEnum.lc.getValue(), "意见回复", JcmcysEnum.blue.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), qmr, null));
        }
        studyFormFillJcgjService.saveBatch(studyFormFillJcgjs);
    }

    /**
     * 更新字段勾选记录
     */
    @Override
    public void updateZdgxjl(StudyFormUpdateForm form) {
        //更新勾选记录
       baseMapper.updateZdgxjl(form.getId(), form.getZdgxjl());
    }

    /**
     * 生成编号
     * @param studyFormFill
     * @return
     */
    public String getSn(StudyFormFill studyFormFill){
        Study study=studyService.getById(studyFormFill.getStudyId());
        if(study==null){
            throw new ServiceException("试验不存在或已删除");
        }
        Template template=templateService.getById(studyFormFill.getTemplateId());
        if(template==null){
            throw new ServiceException("模板不存在或已删除");
        }
        SysDept dep=sysDeptService.selectDeptById(template.getDeptId());
        if(dep==null){
            throw new ServiceException("学科不存在或已删除");
        }
        // MB+表单名缩写(底层模板名称)+版本号+试验编号+学科缩写+流水号+流水号
        return "MB"+template.getShowSn()+study.getSn()+dep.getAbbr()+snGenService.getNewSn(SnTypeEnum.ybbd.getValue())+snGenService.getNewSn(SnTypeEnum.ybbd.getValue());
    }


    /**
     * 获取通知的跳转url
     * @param study
     * @param tab
     * @return
     */
    private String getUrlQz(Study study,String tab){
        return  study.getType().equals(StudyTypeEnum.sy.getValue())?("/study/enter/"+study.getId()+"/"+tab):study.getType().equals(StudyTypeEnum.fsy.getValue())?("/nonTrial/enter/"+study.getId()+"/"+tab):study.getType().equals(StudyTypeEnum.mjy.getValue())?("/drug/enter/"+study.getId()+"/"+tab):"";
    }

}
