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.StudyFormPreListDto;
import com.hxhq.business.enums.SnTypeEnum;
import com.hxhq.business.enums.study.StudyFormPreBdztEnum;
import com.hxhq.business.enums.study.StudyFormPreFzrshztEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.enums.zykgl.JcgjlxEnum;
import com.hxhq.business.enums.zykgl.JcmcysEnum;
import com.hxhq.business.form.study.StudyFormPreAuditForm;
import com.hxhq.business.form.study.StudyFormPreSearchForm;
import com.hxhq.business.form.study.StudyFormUpdateForm;
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.StudyFormPreMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 试验-预填单Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-29
 */
@Service
public class StudyFormPreServiceImpl extends ServiceImpl<StudyFormPreMapper, StudyFormPre> implements IStudyFormPreService {
    @Autowired
    private IStudyFormPreJcgjService studyFormPreJcgjService;
    @Autowired
    private IStudyFormPreQmxxService studyFormPreQmxxService;
    @Autowired
    private IStudyFormFillService studyFormFillService;
    @Autowired
    private IStudyFormFillJcgjService studyFormFillJcgjService;
    @Autowired
    private IStudyFormFillQmxxService studyFormFillQmxxService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISnGenService snGenService;
    @Autowired
    private ITemplateService templateService;
    @Autowired
    private IStudyService studyService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private INoticeService noticeService;

    /**
     * 查询试验-预填单列表
     *
     * @param form 试验-预填单
     * @return 试验-预填单
     */
    @Override
    public List<StudyFormPreListDto> queryList(StudyFormPreSearchForm form) {
        QueryWrapper<StudyFormPre> 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.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.getStartDate())) {
            queryWrapper.apply("t.create_time>={0}", form.getStartDate());
        }
        if (StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.apply("t.create_time<{0}", form.getEndDate());
        }
        queryWrapper.orderByDesc("t.create_time");
        return baseMapper.queryList(queryWrapper);
    }

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

    /**
     * 保存
     *
     * @param studyFormPre
     */
    @Override
    public StudyFormPre bc(StudyFormPre studyFormPre) {
        StudyFormPre result = null;
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        if (studyFormPre.getId() != null) {
            StudyFormPre studyFormPreOld = this.getById(studyFormPre.getId());
            if (studyFormPreOld == null) {
                throw new ServiceException("信息不存在或已删除");
            }
            if (!qmr.getUserId().equals(studyFormPreOld.getUserId())) {
                throw new ServiceException("只能操作自己的表单");
            }
            studyFormPreOld.setBdmc(studyFormPre.getBdmc());
            studyFormPreOld.setTemplateId(studyFormPre.getTemplateId());
            studyFormPreOld.setBdnr(studyFormPre.getBdnr());
            this.updateById(studyFormPreOld);
            List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(studyFormPreOld, studyFormPre);
            if (fieldChanges.size() == 0) {
                //稽查轨迹
                List<StudyFormPreJcgj> studyFormPreJcgjs = new ArrayList<>();
                for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
                    studyFormPreJcgjs.add(studyFormPreJcgjService.getJcgj(studyFormPre.getId(), JcgjlxEnum.xg.getValue(), "修改", JcmcysEnum.orange.getValue(), fieldChange.toString(), fieldChange.toEnString(), SecurityUtils.getLoginUser().getSysUser(), studyFormPre.getRemark()));
                }
                studyFormPreJcgjService.saveBatch(studyFormPreJcgjs);
            }
            result = studyFormPreOld;
        } else {
            studyFormPre.setBdbh(getSn(studyFormPre));
            studyFormPre.setUserId(qmr.getUserId());
            studyFormPre.setUserMc(qmr.getNickName());
            studyFormPre.setUserMcEn(qmr.getNickName());
            studyFormPre.setBdzt(StudyFormPreBdztEnum.tbz.getValue());
            this.save(studyFormPre);
            //稽查轨迹
            studyFormPreJcgjService.saveJcgj(studyFormPre.getId(), JcgjlxEnum.lc.getValue(), "制作保存预制表单", JcmcysEnum.green.getValue(), null, qmr, studyFormPre.getRemark(), null);
            result = studyFormPre;
        }
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPre.getId(), "制作保存预制表单", qmr, studyFormPre.getRemark());
        return baseMapper.queryInfo(result.getId());
    }

    /**
     * 仅保存
     *
     * @param studyFormPre
     */
    @Override
    public StudyFormPre bcOnly(StudyFormPre studyFormPre) {
        StudyFormPre result = null;
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        if (studyFormPre.getId() != null) {
            StudyFormPre studyFormPreOld = this.getById(studyFormPre.getId());
            if (studyFormPreOld == null) {
                throw new ServiceException("信息不存在或已删除");
            }
            if (!qmr.getUserId().equals(studyFormPreOld.getUserId())) {
                throw new ServiceException("只能操作自己的表单");
            }
            studyFormPreOld.setBdmc(studyFormPre.getBdmc());
            studyFormPreOld.setTemplateId(studyFormPre.getTemplateId());
            studyFormPreOld.setBdnr(studyFormPre.getBdnr());
            this.updateById(studyFormPreOld);
            result = studyFormPreOld;
        } else {
            studyFormPre.setBdbh(getSn(studyFormPre));
            studyFormPre.setUserId(qmr.getUserId());
            studyFormPre.setUserMc(qmr.getNickName());
            studyFormPre.setUserMcEn(qmr.getNickName());
            studyFormPre.setBdzt(StudyFormPreBdztEnum.tbz.getValue());
            this.save(studyFormPre);
            result = studyFormPre;
        }
        return baseMapper.queryInfo(result.getId());
    }

    /**
     * 提交
     *
     * @param studyFormPre
     */
    @Override
    public void tj(StudyFormPre studyFormPre) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, studyFormPre.getQmrmm());
        if (studyFormPre.getId() != null) {
            StudyFormPre studyFormPreOld = this.getById(studyFormPre.getId());
            if (studyFormPreOld == null) {
                throw new ServiceException("信息不存在或已删除");
            }
            if (!SecurityUtils.getUserId().equals(studyFormPreOld.getUserId())) {
                throw new ServiceException("只能操作自己的表单");
            }
            studyFormPre.setBdzt(StudyFormPreBdztEnum.ytj.getValue());
            studyFormPre.setTjsj(new Date());
            this.updateById(studyFormPre);
        } else {
            studyFormPre.setBdbh(getSn(studyFormPre));
            studyFormPre.setUserId(qmr.getUserId());
            studyFormPre.setUserMc(qmr.getNickName());
            studyFormPre.setUserMcEn(qmr.getUserName());
            studyFormPre.setTjsj(new Date());
            studyFormPre.setBdzt(StudyFormPreBdztEnum.ytj.getValue());
            this.save(studyFormPre);
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", studyFormPre.getRemark());
        studyFormPreJcgjService.saveJcgj(studyFormPre.getId(), JcgjlxEnum.lc.getValue(), "制作提交预制表单", JcmcysEnum.green.getValue(), null, qmr, studyFormPre.getRemark(), null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPre.getId(), "制作提交预制表单", qmr, studyFormPre.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormPre.getStudyId());
        String url = getUrlQz(study,"ytbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下有预填表单【"+studyFormPre.getBdmc()+"】待审核，请及时处理";
        noticeService.save(title,studyFormPre.getShryId(),url);
    }


    /**
     * 通过
     *
     * @param form
     */
    @Override
    public void tg(StudyFormPreAuditForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, form.getQmrmm());
        StudyFormPre studyFormPreOld = this.getById(form.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("信息不存在或已删除");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.ytj.getValue())) {
            throw new ServiceException("不是已提交状态，不能审核");
        }

        //负责人审核
        if (studyFormPreOld.getFzrshzt().equals(StudyFormPreFzrshztEnum.shz.getValue())) {
            studyFormPreOld.setBdzt(StudyFormPreBdztEnum.ytg.getValue());
            studyFormPreOld.setFzrshzt(StudyFormPreFzrshztEnum.ysh.getValue());
            //发送通知
            Study study = studyService.getById(studyFormPreOld.getStudyId());
            String url = getUrlQz(study,"ytbd");
            String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】已审核通过";
            noticeService.save(title,studyFormPreOld.getUserId(),url);
        } else {
            //权限审核
            String yes = "是";
            if (studyFormPreOld.getFzrsh().equals(yes)) {
                //是否需要负责人审核
                studyFormPreOld.setFzrshzt(StudyFormPreFzrshztEnum.shz.getValue());
                //发送通知
                Study study = studyService.getById(studyFormPreOld.getStudyId());
                String url = getUrlQz(study,"ytbd");
                String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下有预填表单【"+studyFormPreOld.getBdmc()+"】待审核，请及时处理";
                noticeService.save(title,study.getLeader(),url);
            } else {
                studyFormPreOld.setBdzt(StudyFormPreBdztEnum.ytg.getValue());
                //发送通知
                Study study = studyService.getById(studyFormPreOld.getStudyId());
                String url = getUrlQz(study,"ytbd");
                String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】已审核通过";
                noticeService.save(title,studyFormPreOld.getUserId(),url);
            }
        }
        studyFormPreOld.setSjshryId(qmr.getUserId());
        studyFormPreOld.setSjshryMc(qmr.getNickName());
        studyFormPreOld.setSjshryMcEn(qmr.getUserName());
        this.updateById(studyFormPreOld);
        //稽查轨迹
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "审核通过", JcmcysEnum.green.getValue(), null, qmr, form.getRemark(), null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPreOld.getId(), "审核通过", qmr, form.getRemark());
    }

    /**
     * 拒绝
     *
     * @param form
     */
    @Override
    public void jj(StudyFormPreAuditForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, form.getQmrmm());
        StudyFormPre studyFormPreOld = this.getById(form.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("信息不存在或已删除");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.ytj.getValue())) {
            throw new ServiceException("不是已提交状态，不能审核");
        }
        studyFormPreOld.setBdzt(StudyFormPreBdztEnum.wtg.getValue());
        this.updateById(studyFormPreOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "审核拒绝", JcmcysEnum.red.getValue(), formData, qmr, null, null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPreOld.getId(), "审核拒绝", qmr, form.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormPreOld.getStudyId());
        String url = getUrlQz(study,"ytbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】审核未通过";
        noticeService.save(title,studyFormPreOld.getUserId(),url);
    }

    /**
     * 填报
     *
     * @param studyFormPre
     */
    @Override
    public StudyFormFill tb(StudyFormPre studyFormPre) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, studyFormPre.getQmrmm());
        StudyFormPre studyFormPreOld = this.getById(studyFormPre.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("信息不存在或已删除");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.ytg.getValue())) {
            throw new ServiceException("不是已通过状态，不能填报");
        }
        Date time = new Date();
        //稽查轨迹
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "创建记录", JcmcysEnum.green.getValue(), null, qmr, null, null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPreOld.getId(), "创建记录", qmr, studyFormPre.getRemark());
        //生成填报表单
        StudyFormFill studyFormFill = new StudyFormFill();
        studyFormFill.setStudyId(studyFormPreOld.getStudyId());
        studyFormFill.setUserId(qmr.getUserId());
        studyFormFill.setUserMc(qmr.getNickName());
        studyFormFill.setUserMcEn(qmr.getNickName());
        studyFormFill.setStudySubjectId(studyFormPreOld.getStudySubjectId());
        studyFormFill.setBdbh(studyFormPreOld.getBdbh());
        studyFormFill.setBdmc(studyFormPreOld.getBdmc());
        studyFormFill.setBdsm(studyFormPreOld.getBdsm());
        studyFormFill.setBdnr(studyFormPreOld.getBdnr());
        studyFormFill.setStartDate(time);
        studyFormFill.setTemplateId(studyFormPreOld.getTemplateId());
        studyFormFillService.bcAuto(studyFormFill);
        return studyFormFill;
    }

    /**
     * 申请关闭
     *
     * @param studyFormPre
     */
    @Override
    public void sqgb(StudyFormPre studyFormPre) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, studyFormPre.getQmrmm());
        if (studyFormPre.getId() == null || studyFormPre.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        StudyFormPre studyFormPreOld = this.getById(studyFormPre.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("表单不存在或已删除");
        }
        if (!studyFormPreOld.getUserId().equals(qmr.getUserId())) {
            throw new ServiceException("只能操作自己的表单");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.ytg.getValue())) {
            throw new ServiceException("表单不是已通过状态，不能关闭");
        }
        studyFormPreOld.setBdzt(StudyFormPreBdztEnum.dgb.getValue());
        this.updateById(studyFormPreOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", studyFormPre.getRemark());
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "申请关闭", JcmcysEnum.green.getValue(), formData, qmr, null, null);
        //签名信息
        studyFormFillQmxxService.saveQmxx(studyFormPreOld.getId(), "申请关闭", qmr, studyFormPre.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormPreOld.getStudyId());
        String url = getUrlQz(study,"ytbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】申请关闭";
        noticeService.save(title,studyFormPreOld.getSjshryId(),url);
    }


    /**
     * 关闭拒绝
     *
     * @param form
     */
    @Override
    public void jjgb(StudyFormPreAuditForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, form.getQmrmm());
        StudyFormPre studyFormPreOld = this.getById(form.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("信息不存在或已删除");
        }
        if (!studyFormPreOld.getSjshryId().equals(qmr.getUserId())) {
            throw new ServiceException("只能操作自己审核的表单");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.dgb.getValue())) {
            throw new ServiceException("不是待关闭状态，不能审核");
        }
        studyFormPreOld.setBdzt(StudyFormPreBdztEnum.ytg.getValue());
        this.updateById(studyFormPreOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "关闭拒绝", JcmcysEnum.red.getValue(), formData, qmr, null, null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPreOld.getId(), "关闭拒绝", qmr, form.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormPreOld.getStudyId());
        String url = getUrlQz(study,"ytbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】关闭拒绝";
        noticeService.save(title,studyFormPreOld.getUserId(),url);
    }

    /**
     * 关闭同意
     *
     * @param form
     */
    @Override
    public void tygb(StudyFormPreAuditForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证密码
        checkPassword(qmr, form.getQmrmm());
        StudyFormPre studyFormPreOld = this.getById(form.getId());
        if (studyFormPreOld == null) {
            throw new ServiceException("信息不存在或已删除");
        }
        if (!studyFormPreOld.getSjshryId().equals(qmr.getUserId())) {
            throw new ServiceException("只能操作自己审核的表单");
        }
        if (!studyFormPreOld.getBdzt().equals(StudyFormPreBdztEnum.dgb.getValue())) {
            throw new ServiceException("不是待关闭状态，不能审核");
        }
        studyFormPreOld.setBdzt(StudyFormPreBdztEnum.ygb.getValue());
        this.updateById(studyFormPreOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        studyFormPreJcgjService.saveJcgj(studyFormPreOld.getId(), JcgjlxEnum.lc.getValue(), "关闭同意", JcmcysEnum.green.getValue(), formData, qmr, null, null);
        //签名信息
        studyFormPreQmxxService.saveQmxx(studyFormPreOld.getId(), "关闭同意", qmr, form.getRemark());
        //发送通知
        Study study = studyService.getById(studyFormPreOld.getStudyId());
        String url = getUrlQz(study,"ytbd");
        String title = StudyTypeEnum.getEnumByValue(study.getType()).getText()+"【"+study.getName()+"】下预填表单【"+studyFormPreOld.getBdmc()+"】关闭同意";
        noticeService.save(title,studyFormPreOld.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<StudyFormPreJcgj> studyFormPreJcgjs = 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"));
                studyFormPreJcgjs.add(studyFormPreJcgjService.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());
                studyFormPreJcgjs.add(studyFormPreJcgjService.getJcgj(form.getId(), JcgjlxEnum.xg.getValue(), "填写", JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), null, null));
            }
            studyFormPreJcgjService.saveBatch(studyFormPreJcgjs);
        }

    }

    /**
     * 更新复核意见
     */
    @Override
    public void updateFhyjjl(StudyFormUpdateForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //更新复核意见
        baseMapper.updateFhyjjl(form.getId(), form.getFhyjjl());
        StudyFormPre studyFormPreOld = this.queryInfo(form.getId());
        studyFormPreOld.setFhyjjl(form.getFhyjjl());
        this.updateById(studyFormPreOld);
        Map<String, String> formData = new LinkedHashMap<>();
        Map<String, String> formDataEn = new LinkedHashMap<>();
        List<StudyFormPreJcgj> studyFormPreJcgjs = 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"));
            studyFormPreJcgjs.add(studyFormPreJcgjService.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"));
            studyFormPreJcgjs.add(studyFormPreJcgjService.getJcgj(form.getId(), JcgjlxEnum.lc.getValue(), "意见回复", JcmcysEnum.blue.getValue(), JctUtil.formatStr(formData), StudyFormUtil.getJcnrEn(formDataEn), qmr, null));
        }
        studyFormPreJcgjService.saveBatch(studyFormPreJcgjs);
    }

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

    /**
     * 生成编号
     * @param studyFormPre
     * @return
     */
    public String getSn(StudyFormPre studyFormPre){
        Study study=studyService.getById(studyFormPre.getStudyId());
        if(study==null){
            throw new ServiceException("试验不存在或已删除");
        }
        Template template=templateService.getById(studyFormPre.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());
    }

    /**
     * 获取通知的跳转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):"";
    }
}
