package com.hxhq.business.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.json.JSONUtil;
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.mjy.DetailDto;
import com.hxhq.business.dto.mjy.ListDto;
import com.hxhq.business.dto.study.StudyFormFillCc;
import com.hxhq.business.dto.study.StudyFormFillCz;
import com.hxhq.business.dto.study.StudyFormFillResource;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.form.mjy.*;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.business.utils.lang.MjyJcnrUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.DateUtils;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysUser;
import com.hxhq.system.service.ISysUserService;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.MjyMapper;
import com.hxhq.business.domain.Mjy;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 麻精药Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-18
 */
@Service
public class MjyServiceImpl extends ServiceImpl<MjyMapper, Mjy> implements IMjyService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMjyJcgjService mjyJcgjService;
    @Autowired
    private IMjyFfjlService mjyFfjlService;
    @Autowired
    private IZcgService zcgService;
    @Autowired
    private IMjyTzService mjyTzService;
    @Autowired
    private IArchiveLogService archiveLogService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IStudyMjyService studyMjyService;

    //region 资源库管理

    /**
     * 列表
     *
     * @param form 暂存柜
     * @return 暂存柜
     */
    @Override
    public List<ListDto> queryStudyList(SearchForm form) {
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.eq("ts.del_flag", "0");
        queryWrapper.eq("ts.study_id", form.getStudyId());
        if (form.getJyzt() != null && form.getJyzt().intValue() > 0) {
            queryWrapper.eq("t.jyzt", form.getJyzt());
        }
        if (form.getZjzt() != null && form.getZjzt().intValue() > 0) {
            queryWrapper.eq("t.zjzt", form.getZjzt());
        }
        if (StringUtils.isNoneBlank(form.getMc())) {
            queryWrapper.and(p -> p.like("t.`mc`", form.getMc()));
        }
        if (StringUtils.isNoneBlank(form.getBh())) {
            queryWrapper.and(p -> p.like("t.`bh`", form.getBh()));
        }
        if (StringUtils.isNoneBlank(form.getStartDate())) {
            queryWrapper.apply("t.sxrq>={0}", form.getStartDate());
        }
        if (StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.apply("t.sxrq<{0}", form.getEndDate());
        }
        queryWrapper.orderByDesc("ts.id");
        return baseMapper.queryStudyList(queryWrapper);
    }

    /**
     * 列表
     *
     * @param form 暂存柜
     * @return 暂存柜
     */
    @Override
    public List<ListDto> queryList(SearchForm form) {
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.eq("t.del_flag", "0");
        if (form.getJyzt() != null && form.getJyzt().intValue() > 0) {
            queryWrapper.eq("t.jyzt", form.getJyzt());
        }
        if (form.getZjzt() != null && form.getZjzt().intValue() > 0) {
            queryWrapper.eq("t.zjzt", form.getZjzt());
        }
        if (StringUtils.isNoneBlank(form.getMc())) {
            queryWrapper.and(p -> p.like("t.`mc`", form.getMc()));
        }
        if (StringUtils.isNoneBlank(form.getBh())) {
            queryWrapper.and(p -> p.like("t.`bh`", form.getBh()));
        }
        if (StringUtils.isNoneBlank(form.getStartDate())) {
            queryWrapper.apply("t.sxrq>={0}", form.getStartDate());
        }
        if (StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.apply("t.sxrq<{0}", form.getEndDate());
        }
        queryWrapper.orderByDesc("t.id");
        return baseMapper.queryList(queryWrapper);
    }


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

    /**
     * 解档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jd(JdForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if(!mjyOld.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】未归档，不能解档");
        }
        //申请解档后，制剂状态更新为待解档，等待档案员审核期间无法进行其他操作；
        mjyOld.setZjzt(ZjztEnum.djd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "申请解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "申请解档");
    }

    /**
     * 借阅 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jy(JyForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if(!mjyOld.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是归档状态，不能借阅");
        }
        if (!mjyOld.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是未借阅状态，不能借阅");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        mjyOld.setJyzt(JyztEnum.djy.getValue());
        mjyOld.setJyksrq(form.getStartDate());
        mjyOld.setJyjsrq(form.getEndDate());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getStartDate()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getEndDate()));
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", JcmcysEnum.orange.getValue(),formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "申请借阅");
    }

    /**
     * 批量归档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plgd(GdForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Mjy> mjyList = this.list(queryWrapper);
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        if (mjyList.size() > 0) {
            for (Mjy mjy : mjyList) {
                if (!mjy.getZjzt().equals(ZjztEnum.ysd.getValue())) {
                    throw new ServiceException("麻精药【" + mjy.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (Mjy mjy : mjyList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                mjy.setZjzt(ZjztEnum.dgd.getValue());
                mjy.setDazt(DaztEnum.ygd.getValue());
                mjy.setGdsqrId(qmr.getUserId());
                mjy.setGdsqrMc(qmr.getNickName());
                //稽查轨迹
                mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark()));
                //保存日志
                archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjy.getId(), mjy.getMc(), mjy.getBh(), mjy.getNd()+mjy.getNddw(), "申请归档");
            }
            this.updateBatchById(mjyList);
            mjyJcgjService.saveBatch(mjyJcgjList);
        }
    }

    /**
     * 归档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gd(GdForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        mjyOld.setZjzt(ZjztEnum.dgd.getValue());
        mjyOld.setDazt(DaztEnum.ygd.getValue());
        mjyOld.setGdsqrId(qmr.getUserId());
        mjyOld.setGdsqrMc(qmr.getNickName());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "申请归档");
    }


    /**
     * 批量锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plsd(SdForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Mjy> mjyList = this.list(queryWrapper);
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        if (mjyList.size() <= 0) {
            throw new ServiceException("麻精药不是入库状态，不能锁定");
        }
        for (Mjy mjy : mjyList) {
            //锁定
            mjy.setZjzt(ZjztEnum.ysd.getValue());
            //稽查轨迹
            mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "锁定麻精药", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark()));
        }
        this.updateBatchById(mjyList);
        mjyJcgjService.saveBatch(mjyJcgjList);

    }

    /**
     * 锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sd(SdForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库状态，不能锁定");
        }
        mjyOld.setZjzt(ZjztEnum.ysd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "锁定麻精药", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark());
    }


    /**
     * 解锁 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void js(JsForm form) {
        SysUser qmr = SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        mjyOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "解锁麻精药", JcmcysEnum.orange.getValue(),null,qmr, form.getRemark());
    }

    /**
     * 批量处置药剂 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plczyj(PlczyjForm form) {
        SysUser clr = SecurityUtils.getLoginUser().getSysUser();
        SysUser fhr = sysUserService.selectUserById(form.getFhrId());
        SysUser jdr = sysUserService.selectUserById(form.getJdrId());
        //验证处理人密码
        checkPassword(clr, form.getClrmm(),true);
        //验证复核人密码
        checkPassword(fhr, form.getFhrmm(),true);
        //验证监督人密码
        checkPassword(jdr, form.getJdrmm(),true);

        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Mjy> mjyList = this.list(queryWrapper);
        if (mjyList.size() <= 0) {
            throw new ServiceException("麻精药不是入库状态，不能处置药剂");
        }
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        List<MjyTz> mjyTzList = new ArrayList<>();
        for (Mjy mjy : mjyList) {
            List<PlczyjItem> mjyPlffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(mjy.getId()))
                    .collect(Collectors.toList());
            if (StringUtils.isBlank(mjyPlffItemFilter.get(0).getCzl())) {
                throw new ServiceException("处置量不能为空");
            }
            BigDecimal czl = BigDecimal.ZERO;
            try {
                czl = new BigDecimal(mjyPlffItemFilter.get(0).getCzl());
            } catch (Exception ex) {
                throw new ServiceException("处置量填写不正确");
            }
            if (mjyPlffItemFilter.size() > 0) {
                BigDecimal kc = new BigDecimal(mjy.getKc());
                mjy.setKc(kc.subtract(czl).toString());
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("处置方式", mjyPlffItemFilter.get(0).getCzfs());
                formData.put("处置原因", mjyPlffItemFilter.get(0).getCzyy());
                formData.put("处置量", mjyPlffItemFilter.get(0).getCzl() + mjy.getKcdw());
                formData.put("处置人", clr.getNickName());
                formData.put("复核人", fhr.getNickName());
                formData.put("监督人", jdr.getNickName());
                mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "处置药剂", JcmcysEnum.orange.getValue(),formData,null, null));
                //台账
                mjyTzList.add(mjyTzService.getTz(mjy.getId(),null,null,null,null,null,null,null,null,clr,"处置药剂",mjyPlffItemFilter.get(0).getCzl(),mjy.getKcdw(),mjyPlffItemFilter.get(0).getCzyy()));
            } else {
                throw new ServiceException("麻精药【" + mjy.getMc() + "】不存在或已删除");
            }
        }
        this.updateBatchById(mjyList);
        mjyJcgjService.saveBatch(mjyJcgjList);
        mjyTzService.saveBatch(mjyTzList);
    }


    /**
     * 处置药剂 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void czyj(CzyjForm form) {

        SysUser clr = SecurityUtils.getLoginUser().getSysUser();
        SysUser fhr = sysUserService.selectUserById(form.getFhrId());
        SysUser jdr = sysUserService.selectUserById(form.getJdrId());

        //验证处理人密码
        checkPassword(clr, form.getClrmm(),true);
        //验证复核人密码
        checkPassword(fhr, form.getFhrmm(),true);
        //验证监督人密码
        checkPassword(jdr, form.getJdrmm(),true);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库状态，不能处置药剂");
        }
        if (StringUtils.isBlank(form.getCzl())) {
            throw new ServiceException("处置量不能为空");
        }
        BigDecimal kc = new BigDecimal(mjyOld.getKc());
        mjyOld.setKc(kc.subtract(new BigDecimal(form.getCzl())).toString());

        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("处置方式", form.getCzfs());
        formData.put("处置原因", form.getCzyy());
        formData.put("处置量", form.getCzl() + mjyOld.getKcdw());
        formData.put("处置人", clr.getNickName());
        formData.put("复核人", fhr.getNickName());
        formData.put("监督人", jdr.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "处置药剂", JcmcysEnum.green.getValue(), formData, null, null);

        //台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,null,null,null,null,null,null,clr,"处置药剂",form.getCzl(),mjyOld.getKcdw(),form.getCzyy());
        this.updateById(mjyOld);
    }


    /**
     * 批量处置容器 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plczrq(PlczrqForm form) {
        SysUser clr = SecurityUtils.getLoginUser().getSysUser();
        SysUser fhr = sysUserService.selectUserById(form.getFhrId());
        SysUser jdr = sysUserService.selectUserById(form.getJdrId());
        //验证处理人密码
        checkPassword(clr, form.getClrmm(),true);
        //验证复核人密码
        checkPassword(fhr, form.getFhrmm(),true);
        //验证监督人密码
        checkPassword(jdr, form.getJdrmm(),true);

        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        queryWrapper.eq("rqzt", RqztEnum.wcz.getValue());
        List<Mjy> mjyList = this.list(queryWrapper);
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        List<MjyTz> mjyTzList = new ArrayList<>();
        if (mjyList.size() <= 0) {
            throw new ServiceException("麻精药已经处置过容器了，不能重复操作");
        }
        for (Mjy mjy : mjyList) {
            List<PlczrqItem> mjyPlffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(mjy.getId()))
                    .collect(Collectors.toList());
            if (mjyPlffItemFilter.size() > 0) {
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("处置方式", mjyPlffItemFilter.get(0).getCzfs());
                formData.put("处置原因", mjyPlffItemFilter.get(0).getCzyy());
                formData.put("处置人", clr.getNickName());
                formData.put("复核人", fhr.getNickName());
                formData.put("监督人", jdr.getNickName());
                mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "处置容器", JcmcysEnum.green.getValue(), formData,null , null));
                //台账
                mjyTzList.add(mjyTzService.getTz(mjy.getId(),null,null,null,null,null,null,null,null,clr,"处置容器",null,null,mjyPlffItemFilter.get(0).getCzyy()));
                mjy.setRqzt(RqztEnum.ycz.getValue());

            } else {
                throw new ServiceException("麻精药【" + mjy.getMc() + "】不存在或已删除");
            }
        }
        this.updateBatchById(mjyList);
        mjyJcgjService.saveBatch(mjyJcgjList);
        mjyTzService.saveBatch(mjyTzList);
    }

    /**
     * 处置容器 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void czrq(CzrqForm form) {
        SysUser clr = SecurityUtils.getLoginUser().getSysUser();
        SysUser fhr = sysUserService.selectUserById(form.getFhrId());
        SysUser jdr = sysUserService.selectUserById(form.getJdrId());
        //验证处理人密码
        checkPassword(clr, form.getClrmm(),true);
        //验证复核人密码
        checkPassword(fhr, form.getFhrmm(),true);
        //验证监督人密码
        checkPassword(jdr, form.getJdrmm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库状态，不能处置容器");
        }
        if (!mjyOld.getRqzt().equals(RqztEnum.wcz.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】已经处置过容器了，不能重复操作");
        }
        mjyOld.setRqzt(RqztEnum.ycz.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("处置方式", form.getCzfs());
        formData.put("处置原因", form.getCzyy());
        formData.put("处置人", clr.getNickName());
        formData.put("复核人", fhr.getNickName());
        formData.put("监督人", jdr.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "处置容器", JcmcysEnum.green.getValue(), formData,  null,null);

        //台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,null,null,null,null,null,null,clr,"处置容器",null,null,form.getCzyy());
    }


    /**
     * 钥匙发放 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ysff(YsffForm form) {
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        SysUser lqr2 = sysUserService.selectUserById(form.getLqr2Id());
        SysUser ffr1 = SecurityUtils.getLoginUser().getSysUser();
        //验证领取人1密码
        checkPassword(lqr1, form.getLqr1mm(),true);
        //验证领取人2密码
        checkPassword(lqr2, form.getLqr2mm(),true);
        //验证发放人密码
        checkPassword(ffr1, form.getFfrmm(),true);

        Zcg zcg = zcgService.getById(form.getZcgId());
        if (zcg == null) {
            throw new ServiceException("暂存柜不存在或已删除");
        }
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Mjy> mjyList = this.list(queryWrapper);
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        if (mjyList.size() <= 0) {
            throw new ServiceException("药剂已被发放,请不要重复发放");
        }
        QueryWrapper<MjyFfjl> mjyFfjlQueryWrapper = Wrappers.query();
        mjyFfjlQueryWrapper.in("mjy_id", form.getIds());
        List<MjyFfjl> mjyFfjlList = mjyFfjlService.list(mjyFfjlQueryWrapper);
        for (MjyFfjl mjyFfjl : mjyFfjlList) {
            mjyFfjl.setZcgId(zcg.getId());
        }
        String glyj = "";
        for (Mjy mjy : mjyList) {
            //更新暂存柜id
            mjy.setZcgId(zcg.getId());
            //更新暂存柜关联药剂
            if (StringUtils.isBlank(glyj)) {
                glyj += mjy.getMc();
            } else {
                glyj += "," + mjy.getMc();
            }
            //稽查轨迹
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("钥匙1领取人", lqr1.getNickName());
            formData.put("钥匙2领取人", lqr2.getNickName());
            formData.put("发放人", ffr1.getNickName());
            mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "钥匙发放", JcmcysEnum.green.getValue(), formData,  null,null));
        }
        if (StringUtils.isBlank(zcg.getGlyj())) {
            zcg.setGlyj(glyj);
        } else {
            zcg.setGlyj(zcg.getGlyj() + "," + glyj);
        }
        zcg.setLqr1Id(lqr1.getUserId());
        zcg.setLqr1Mc(lqr1.getNickName());
        zcg.setLqr2Id(lqr2.getUserId());
        zcg.setLqr2Mc(lqr2.getNickName());
        zcg.setZt(zcgJyztEnum.jyz.getValue());
        zcgService.updateById(zcg);
        this.updateBatchById(mjyList);
        mjyJcgjService.saveBatch(mjyJcgjList);
        if(mjyFfjlList.size()>0){
            mjyFfjlService.updateBatchById(mjyFfjlList);
        }
    }


    /**
     * 发放 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ff(FfForm form) {
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        SysUser lqr2 = sysUserService.selectUserById(form.getLqr2Id());
        SysUser ffr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser ffr2 = sysUserService.selectUserById(form.getFfr2Id());
        //验证领取人1密码
        checkPassword(lqr1, form.getLqr1mm(),true);
        //验证领取人2密码
        checkPassword(lqr2, form.getLqr2mm(),true);
        //验证发放人1密码
        checkPassword(ffr1, form.getFfr1mm(),true);
        //验证发放人2密码
        checkPassword(ffr2, form.getFfr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库状态，不能发放");
        }
        Date now = new Date();

        //region 发放记录，试验物资 todo
        mjyFfjlService.ff(form, mjyOld, lqr1, lqr2, ffr1, ffr2, now);
        //endregion

        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("出库量", mjyOld.getKc() + mjyOld.getKcdw());
        formData.put("转移条件", form.getZytj());
        formData.put("出库毛重", form.getCkmz() + mjyOld.getKcdw());
        formData.put("目的", form.getMdMcs());
        formData.put("领取人", lqr1.getNickName());
        formData.put("领取人2", lqr2.getNickName());
        formData.put("发放人", ffr1.getNickName());
        formData.put("发放人2", ffr2.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "领取发放", JcmcysEnum.green.getValue(), formData,  ffr1,form.getRemark());
        //台账
        mjyTzService.saveTz(mjyOld.getId(),lqr1,lqr2,null,null,ffr1,ffr2,null,null,null,"领取发放",mjyOld.getKc(),mjyOld.getKcdw(),form.getRemark());

        //更新发放信息
        mjyOld.setCkl(mjyOld.getKc());
        mjyOld.setCkldw(mjyOld.getKcdw());
        mjyOld.setFfzytj(form.getZytj());
        mjyOld.setCkmz(form.getCkmz());
        mjyOld.setCkmzdw(form.getCkmzdw());
        mjyOld.setLqr1Id(lqr1.getUserId());
        mjyOld.setLqr2Id(lqr2.getUserId());
        mjyOld.setFfr1Id(ffr1.getUserId());
        mjyOld.setFfr2Id(ffr2.getUserId());
        mjyOld.setLqr1Mc(lqr1.getNickName());
        mjyOld.setLqr2Mc(lqr2.getNickName());
        mjyOld.setFfr1Mc(ffr1.getNickName());
        mjyOld.setFfr2Mc(ffr2.getNickName());
        mjyOld.setFfrq(now);
        mjyOld.setFfbz(form.getRemark());
        mjyOld.setMdIds(form.getMdIds());
        mjyOld.setZjzt(ZjztEnum.yff.getValue());
        this.updateById(mjyOld);
    }


    /**
     * 批量发放 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plff(PlffForm form) {
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        SysUser lqr2 = sysUserService.selectUserById(form.getLqr2Id());
        SysUser ffr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser ffr2 = sysUserService.selectUserById(form.getFfr2Id());
        //验证领取人1密码
        checkPassword(lqr1, form.getLqr1mm(),true);
        //验证领取人2密码
        checkPassword(lqr2, form.getLqr2mm(),true);
        //验证发放人1密码
        checkPassword(ffr1, form.getFfr1mm(),true);
        //验证发放人2密码
        checkPassword(ffr2, form.getFfr2mm(),true);

        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Mjy> mjyList = this.list(queryWrapper);
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        List<MjyTz> mjyTzList = new ArrayList<>();
        if (mjyList.size() <= 0) {
            throw new ServiceException("麻精药不是入库状态，不能发放");
        }
        Date now = new Date();
        for (Mjy mjy : mjyList) {
            //验证库存
            BigDecimal kc = new BigDecimal(mjy.getKc());
            if (kc.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("麻精药【" + mjy.getMc() + "】库存不足，不能发放");
            }
            List<FfForm> plffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(mjy.getId()))
                    .collect(Collectors.toList());
            if (plffItemFilter.size() > 0) {
                //发放 todo
                mjyFfjlService.ff(plffItemFilter.get(0), mjy, lqr1, lqr2, ffr1, ffr2, now);
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("出库量", mjy.getKc() + mjy.getKcdw());
                formData.put("转移条件", plffItemFilter.get(0).getZytj());
                formData.put("出库毛重", plffItemFilter.get(0).getCkmz() + mjy.getKcdw());
                formData.put("目的", plffItemFilter.get(0).getMdMcs());
                formData.put("领取人", lqr1.getNickName());
                formData.put("领取人2", lqr2.getNickName());
                formData.put("发放人", ffr1.getNickName());
                formData.put("发放人2", ffr2.getNickName());
                mjyJcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "领取发放", JcmcysEnum.green.getValue(), formData,  ffr1,plffItemFilter.get(0).getRemark()));
                //台账
                mjyTzList.add(mjyTzService.getTz(mjy.getId(),lqr1,lqr2,null,null,ffr1,ffr2,null,null,null,"领取发放",mjy.getKc(),mjy.getKcdw(),plffItemFilter.get(0).getRemark()));
                //更新发放信息
                mjy.setCkl(mjy.getKc());
                mjy.setCkldw(mjy.getKcdw());
                mjy.setFfzytj(plffItemFilter.get(0).getZytj());
                mjy.setCkmz(plffItemFilter.get(0).getCkmz());
                mjy.setCkmzdw(plffItemFilter.get(0).getCkmzdw());
                mjy.setLqr1Id(lqr1.getUserId());
                mjy.setLqr2Id(lqr2.getUserId());
                mjy.setFfr1Id(ffr1.getUserId());
                mjy.setFfr2Id(ffr2.getUserId());
                mjy.setLqr1Mc(lqr1.getNickName());
                mjy.setLqr2Mc(lqr2.getNickName());
                mjy.setFfr1Mc(ffr1.getNickName());
                mjy.setFfr2Mc(ffr2.getNickName());
                mjy.setFfrq(now);
                mjy.setFfbz(plffItemFilter.get(0).getRemark());
                mjy.setMdIds(plffItemFilter.get(0).getMdIds());
                mjy.setMdOther(plffItemFilter.get(0).getMdOther());
                mjy.setZjzt(ZjztEnum.yff.getValue());
            } else {
                throw new ServiceException("麻精药【" + mjy.getMc() + "】不存在或已删除");
            }
        }
        mjyJcgjService.saveBatch(mjyJcgjList);
        mjyTzService.saveBatch(mjyTzList);
        this.updateBatchById(mjyList);
    }

    /**
     * 编辑 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bj(BjForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getBjzt().equals(BjEnum.wbj.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】已经提交编辑申请，请等待审核");
        }
        if (!(mjyOld.getZjzt().equals(ZjztEnum.rk.getValue()) || mjyOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库或已发放状态，不能编辑");
        }
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();

        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(mjyOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            mjyJcgjList.add(mjyJcgjService.getJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "申请编辑", JcmcysEnum.green.getValue(), fieldChange.toString(), fieldChange.toEnString(), qmr,null));
        }
        mjyOld.setMcbj(form.getMc());
        mjyOld.setNdbj(form.getNd());
        mjyOld.setNddwbj(form.getNddw());
        mjyOld.setSxrqbj(form.getSxrq());
        mjyOld.setCctjbj(form.getCctj());
        mjyOld.setCcwzbj(form.getCcwz());
        mjyOld.setBjbz(form.getBjbz());
        mjyOld.setBjrId(SecurityUtils.getUserId());
        mjyOld.setBjrMc(SecurityUtils.getNickName());
        mjyOld.setBjzt(BjEnum.shz.getValue());
        this.updateById(mjyOld);
        mjyJcgjService.saveBatch(mjyJcgjList);
    }

    /**
     * 审核编辑 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shbj(ShbjForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getBjzt().equals(BjEnum.shz.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】没有提交编辑申请，不能审核编辑");
        }
        if (!(mjyOld.getZjzt().equals(ZjztEnum.rk.getValue()) || mjyOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库或已发放状态，不能审核编辑");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", mjyOld.getBjbz());
        formData.put("审核备注", form.getRemark());
        if (form.getQmyy().equals(ShbjEnum.ty.getValue())) {
            mjyOld.setCcwz(mjyOld.getCcwzbj());
            mjyOld.setCctj(mjyOld.getCctjbj());
            mjyOld.setSxrq(mjyOld.getSxrqbj());
            mjyOld.setNddw(mjyOld.getNddwbj());
            mjyOld.setNd(mjyOld.getNdbj());
            mjyOld.setMc(mjyOld.getMcbj());
            mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "同意编辑", JcmcysEnum.green.getValue(), formData,  qmr,null);
            //通知
            noticeService.save("麻精药【"+mjyOld.getMc()+"】同意编辑",mjyOld.getBjrId(),getUrlQz("mjyList"));
        } else {
            mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝编辑", JcmcysEnum.red.getValue(), formData,  qmr,null);
            //通知
            noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝编辑",mjyOld.getBjrId(),getUrlQz("mjyList"));
        }
        //清空编辑信息
        mjyOld.setMcbj(null);
        mjyOld.setNddwbj(null);
        mjyOld.setNdbj(null);
        mjyOld.setSxrqbj(null);
        mjyOld.setCctjbj(null);
        mjyOld.setCcwzbj(null);
        mjyOld.setBjbz(null);
        mjyOld.setBjrId(null);
        mjyOld.setBjrMc(null);
        mjyOld.setBjzt(BjEnum.wbj.getValue());
        this.updateById(mjyOld);
    }


    /**
     * 修改库存 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void xgkc(XgkcForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getKcbjzt().equals(KcbjEnum.wbj.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】已经提交修改库存申请，请等待审核");
        }
        if (!(mjyOld.getZjzt().equals(ZjztEnum.rk.getValue()) || mjyOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库或已发放状态，不能修改库存");
        }
        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(mjyOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }
        List<MjyJcgj> mjyJcgjList = new ArrayList<>();
        //region 稽查轨迹
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            mjyJcgjList.add(mjyJcgjService.getJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "修改库存申请", JcmcysEnum.green.getValue(), fieldChange.toString(), fieldChange.toEnString(),  qmr,null));
        }
        //endregion

        mjyOld.setKcdwbj(form.getKcdw());
        mjyOld.setKcbj(form.getKc());
        mjyOld.setKcbjrId(SecurityUtils.getUserId());
        mjyOld.setKcbjrMc(SecurityUtils.getNickName());
        mjyOld.setKcbjzt(KcbjEnum.shz.getValue());
        mjyOld.setKcbjbz(form.getKcbjbz());
        this.updateById(mjyOld);
        mjyJcgjService.saveBatch(mjyJcgjList);

    }


    /**
     * 审核修改库存 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shxgkc(ShxgkcForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr, form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getKcbjzt().equals(KcbjEnum.shz.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】没有提交修改库存申请，不能审核修改库存");
        }
        if (!(mjyOld.getZjzt().equals(ZjztEnum.rk.getValue()) || mjyOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是入库或已发放状态，不能审核修改库存");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", mjyOld.getKcbjbz());
        formData.put("审核备注", form.getRemark());
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "同意修改库存", JcmcysEnum.green.getValue(), formData, qmr,null);
            mjyOld.setKcdw(mjyOld.getKcdwbj());
            mjyOld.setKc(mjyOld.getKcbj());
            //通知
            noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝修改库存",mjyOld.getKcbjrId(),getUrlQz("mjyList"));
        } else {
            mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝修改库存", JcmcysEnum.red.getValue(), formData,  qmr,null);
            //通知
            noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝编辑",mjyOld.getKcbjrId(),getUrlQz("mjyList"));
        }
        //台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,null,null,null,null,null,null,qmr,"修改库存",mjyOld.getKcbj(),mjyOld.getKcdw(),mjyOld.getKcbjbz());

        mjyOld.setKcbj(null);
        mjyOld.setKcdwbj(null);
        mjyOld.setKcbjbz(null);
        mjyOld.setKcbjzt(KcbjEnum.wbj.getValue());
        this.updateById(mjyOld);
    }

    /**
     * 归还 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gh(GhForm form) {
        SysUser ghr1 = sysUserService.selectUserById(form.getGhr1Id());
        SysUser ghr2 = sysUserService.selectUserById(form.getGhr2Id());
        SysUser jsr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser jsr2 = sysUserService.selectUserById(form.getJsr2Id());
        //验证归还人1密码
        checkPassword(ghr1, form.getGhr1mm(),true);
        //验证归还人2密码
        checkPassword(ghr2, form.getGhr2mm(),true);
        //验证接收人1密码
        checkPassword(jsr1, form.getJsr1mm(),true);
        //验证接收人2密码
        checkPassword(jsr2, form.getJsr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是已发放状态，不能归还");
        }
        if (!mjyOld.getCczt().equals(CcztEnum.Qc.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】是暂存状态，请先将该物质从暂存处取出");
        }
        BigDecimal leftKc = new BigDecimal(mjyOld.getKc()).subtract(new BigDecimal(form.getSyl()));
        mjyFfjlService.gh(form, mjyOld, ghr1, ghr2, jsr1, jsr2);
        // 台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,ghr1,ghr2,null,null,jsr1,jsr2,null,"归还",form.getRkmz(),mjyOld.getKcdw(),form.getRemark());
        // 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getGhzytj());
        formData.put("入库毛重", form.getRkmz() + mjyOld.getKcdw());
        formData.put("减少量", form.getJsl() + mjyOld.getKcdw());
        formData.put("使用量", form.getSyl() + mjyOld.getKcdw());
        formData.put("归还人1", ghr1.getNickName());
        formData.put("归还人2", ghr2.getNickName());
        formData.put("库管员1", jsr1.getNickName());
        formData.put("库管员2", jsr2.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "归还", JcmcysEnum.green.getValue(), formData,  null,null);
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】归还",mjyOld.getKcbjrId(),getUrlQz("mjyList"));
        noticeService.save("麻精药【"+mjyOld.getMc()+"】归还",mjyOld.getKcbjrId(),getUrlQz("mjyList"));
        // 更新
        mjyOld.setKc(leftKc.toString());
        mjyOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(mjyOld);
    }



    /**
     * 入库 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rk(RkForm form) {
        SysUser qmr1 = sysUserService.selectUserById(form.getQmr1Id());
        SysUser qmr2 = sysUserService.selectUserById(form.getQmr2Id());
        SysUser jsr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser jsr2 = sysUserService.selectUserById(form.getJsr2Id());
        //验证签名人1密码
        checkPassword(qmr1, form.getQmr1mm(),true);
        //验证签名人2密码
        checkPassword(qmr2, form.getQmr2mm(),true);
        //验证接收人1密码
        checkPassword(jsr1, form.getJsr1mm(),true);
        //验证接收人2密码
        checkPassword(jsr2, form.getJsr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.wrk.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是未入库状态，不能入库");
        }
        // 台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,qmr1,qmr2,null,null,jsr1,jsr2,null,"麻精药入库",form.getRkl(),mjyOld.getKcdw(),form.getRemark());
        // 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getZytj());
        formData.put("入库毛重", form.getRkl() + mjyOld.getKcdw());
        formData.put("签名人1", qmr1.getNickName());
        formData.put("签名人2", qmr2.getNickName());
        formData.put("库管员1", jsr1.getNickName());
        formData.put("库管员2", jsr2.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "麻精药入库", JcmcysEnum.green.getValue(), formData,  null,null);
        // 更新
        mjyOld.setKc(form.getRkl());
        mjyOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(mjyOld);
    }


    /**
     * 存储
     *
     * @param form
     */
    @Override
    public void cc(com.hxhq.business.form.mjy.CcForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        SysUser qmr2=sysUserService.selectUserById(form.getQmr2Id());
        //验证签名人密码
        checkPassword(qmr,form.getQmr1mm(),true);
        checkPassword(qmr2,form.getQmr2mm(),true);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是已发放状态，不能解锁");
        }
        mjyOld.setCczt(CcztEnum.Cc.getValue());
        mjyOld.setCcwz(form.getCcwz());
        mjyOld.setCctj(form.getCctj());
        this.updateById(mjyOld);
        // 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("存储位置", form.getCcwz());
        formData.put("存储条件", form.getCctj());
        formData.put("签名人1", qmr.getNickName());
        formData.put("签名人2", qmr.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "存储", JcmcysEnum.green.getValue(), formData, null, form.getRemark());
        //台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,qmr,qmr2,null,null,null,null,null,"存储",mjyOld.getKc(),mjyOld.getKcdw(),form.getRemark());
    }

    /**
     * 取出
     *
     * @param form
     */
    @Override
    public void qc(com.hxhq.business.form.mjy.CcForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        SysUser qmr2=sysUserService.selectUserById(form.getQmr2Id());
        //验证签名人密码
        checkPassword(qmr,form.getQmr1mm(),true);
        checkPassword(qmr2,form.getQmr2mm(),true);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是已发放状态，不能解锁");
        }
        mjyOld.setCczt(CcztEnum.Qc.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("签名人1", qmr.getNickName());
        formData.put("签名人2", qmr.getNickName());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "取出", JcmcysEnum.green.getValue(), formData, null, form.getRemark());
        //台账
        mjyTzService.saveTz(mjyOld.getId(),null,null,qmr,qmr2,null,null,null,null,null,"取出",mjyOld.getKc(),mjyOld.getKcdw(),form.getRemark());
    }

    /**
     * 表单生成
     * @param studyFormFill
     * @param template
     * @param remark
     */
    @Override
    public void genResource(StudyFormFill studyFormFill, Template template,String remark) {
        System.out.println(System.currentTimeMillis());
        SysUser qmr = sysUserService.selectUserById(SecurityUtils.getUserId());

        Long studyId = studyFormFill.getStudyId();
        Long studyFormId = studyFormFill.getId();
        // 生成/使用
        String resource = studyFormFill.getResource();
        Map<String, Mjy> mjyMap = new HashMap<>();
        List<Mjy> mjyList = new ArrayList<>();
        LinkedList<MjyJcgj> jcgjList = new LinkedList<>();
        LinkedList<MjyTz> tzList = new LinkedList<>();
        if(StringUtils.isNotEmpty(resource) && !StringUtils.equals("[]", resource)) {
            List<StudyFormFillResource> studyFormFillResourceList = JSONUtil.toList(resource, StudyFormFillResource.class);
            List<String> bhList = studyFormFillResourceList.stream().map(StudyFormFillResource::getBh).collect(Collectors.toList());
            // 根据编号获取需要处置的列表
            LambdaQueryWrapper<Mjy> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Mjy::getBh, bhList);
            mjyList = this.list(queryWrapper);
            for (Mjy mjy : mjyList) {
                mjyMap.put(mjy.getBh(), mjy);
            }

            for (StudyFormFillResource studyFormFillResource : studyFormFillResourceList) {
                Mjy mjy = mjyMap.get(studyFormFillResource.getBh());
                if(mjy == null) {
                    mjy = new Mjy();
                    mjy.setStudyId(studyId);
                    mjy.setBdId(studyFormId);
                    mjy.setZjzt(ZjztEnum.wrk.getValue());
                    mjy.setJyzt(JyztEnum.wjy.getValue());
                }
                mjy.setMc(studyFormFillResource.getMc());
                mjy.setBh(studyFormFillResource.getBh());
                mjy.setNd(String.valueOf(studyFormFillResource.getNdz()));
                mjy.setNddw(studyFormFillResource.getNddw());
                mjy.setKc(String.valueOf(studyFormFillResource.getKc()));
                mjy.setKcdw(studyFormFillResource.getKcdw());
                mjy.setSxrq(DateUtils.parseDate(studyFormFillResource.getSxrq()));
                if(mjy.getId() == null) {
                    this.save(mjy);
                    mjyList.add(mjy);
                    mjyMap.put(mjy.getBh(), mjy);
                    // 稽查轨迹
                    jcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "配制完成",JcmcysEnum.green.getValue(), null, null, qmr,remark));
                    // 台账
                    tzList.add(mjyTzService.getTzGen(mjy.getId(), null,null, null,null, null,null, null,null, qmr,"配制完成" ,mjy.getKc(),mjy.getKcdw(), remark,studyFormFill.getId()));
                } else {
                    this.updateById(mjy);
                }
                // 使用
                String syl = studyFormFillResource.getSyl();
                if(StringUtils.isNotEmpty(syl) && NumberUtils.isParsable(syl)) {
                    BigDecimal kc = new BigDecimal(mjy.getKc());
                    kc = kc.subtract(new BigDecimal(syl));
                    mjy.setKc(kc.toString());
                    baseMapper.updateKc(mjy.getId(), kc.toString());
                    // 使用稽查轨迹
                    HashMap<String, String> formData = new LinkedHashMap<>();
                    formData.put("使用量", syl + studyFormFillResource.getSyldw());
                    // 稽查轨迹
                    jcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "使用",JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(MjyJcnrUtil.getMapEn(formData)), qmr,remark));
                    // 台账
                    tzList.add(mjyTzService.getTzGen(mjy.getId(), null,null, null,null, null,null, null,null, qmr,"使用" ,syl,studyFormFillResource.getSyldw(), remark,studyFormFill.getId()));
                }
                mjyMap.put(mjy.getBh(), mjy);
            }
        }

        // 存储
        String cclist = studyFormFill.getCclist();
        if(StringUtils.isNotEmpty(cclist) && !StringUtils.equals("[]", cclist)) {
            List<StudyFormFillCc> studyFormFillCcList = JSONUtil.toList(cclist, StudyFormFillCc.class);
            for (StudyFormFillCc studyFormFillCc : studyFormFillCcList) {
                baseMapper.updateCc(studyFormFillCc.getList(), studyFormFillCc.getCctj(), studyFormFillCc.getCcwz());
                for (String bh : studyFormFillCc.getList()) {
                    Mjy mjy = mjyMap.get(bh);

                    // 稽查轨迹
                    HashMap<String, String> formData = new HashMap<>();
                    formData.put("存储位置", studyFormFillCc.getCcwz());
                    formData.put("存储条件", studyFormFillCc.getCctj());
                    jcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "存储",JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(MjyJcnrUtil.getMapEn(formData)), qmr,remark));
                    // 台账
                    tzList.add(mjyTzService.getTzGen(mjy.getId(), null,null, null,null, null,null, null,null, qmr,"存储" ,mjy.getKc(),mjy.getKcdw(), remark,studyFormFill.getId()));
                }
            }
        }

        // 处置，库存全变为0
        String czlist = studyFormFill.getCzlist();
        if(StringUtils.isNotEmpty(czlist) && !StringUtils.equals("[]", czlist)) {
            List<StudyFormFillCz> studyFormFillCzList = JSONUtil.toList(czlist, StudyFormFillCz.class);
            for (StudyFormFillCz studyFormFillCz : studyFormFillCzList) {
                for (String bh : studyFormFillCz.getList()) {
                    Mjy mjy = mjyMap.get(bh);
                    // 稽查轨迹
                    HashMap<String, String> formData = new HashMap<>();
                    formData.put("处置方式", studyFormFillCz.getCzfs());
                    formData.put("处置量", mjy.getKc() + mjy.getKcdw());
                    jcgjList.add(mjyJcgjService.getJcgj(mjy.getId(), JcgjlxEnum.lc.getValue(), "处置",JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(MjyJcnrUtil.getMapEn(formData)), qmr,remark));

                    // 台账
                    tzList.add(mjyTzService.getTzGen(mjy.getId(), null,null, null,null, null,null, null,null, qmr,"处置" ,mjy.getKc(),mjy.getKcdw(), remark,studyFormFill.getId()));

                }
                baseMapper.updateKcBatch(studyFormFillCz.getList(), "0");
            }
        }
        mjyJcgjService.saveBatch(jcgjList);
        mjyTzService.saveBatch(tzList);
        studyMjyService.saveBatch(studyId, mjyList);

    }

    //endregion

    //region 档案管理

    /**
     * 查询麻精药列表
     *
     * @param form 麻精药
     * @return 麻精药集合
     */
    @Override
    public List<ListDto> queryArchiveList(SearchForm form){
        QueryWrapper<Mjy> queryWrapper = Wrappers.query();
        queryWrapper.eq("t.del_flag", "0");
        queryWrapper.eq("t.`dazt`", DaztEnum.ygd.getValue());
        if (form.getJyzt() != null && form.getJyzt().intValue() > 0) {
            queryWrapper.eq("t.jyzt", form.getJyzt());
        }
        if (form.getZjzt() != null && form.getZjzt().intValue() > 0) {
            queryWrapper.eq("t.zjzt", form.getZjzt());
        }
        if (StringUtils.isNoneBlank(form.getMc())) {
            queryWrapper.and(p -> p.like("t.`mc`", form.getMc()));
        }
        if (StringUtils.isNoneBlank(form.getBh())) {
            queryWrapper.and(p -> p.like("t.`bh`", form.getBh()));
        }
        if (StringUtils.isNoneBlank(form.getStartDate())) {
            queryWrapper.apply("t.sxrq>={0}", form.getStartDate());
        }
        if (StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.apply("t.sxrq<{0}", form.getEndDate());
        }
        queryWrapper.orderByDesc("t.id");
        return baseMapper.queryList(queryWrapper);
    }

    /**
     * 同意归档
     */
    @Override
    public void tygd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待归档状态，不能操作");
        }
        mjyOld.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "同意归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "同意归档");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】同意归档",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 拒绝归档
     */
    @Override
    public void jjgd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待归档状态，不能操作");
        }
        mjyOld.setZjzt(ZjztEnum.ysd.getValue());
        mjyOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "拒绝归档");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝归档",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 同意解档
     */
    @Override
    public void tyjd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待解档状态，不能操作");
        }
        mjyOld.setZjzt(ZjztEnum.ysd.getValue());
        mjyOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "同意解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "同意解档");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】同意解档",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 拒绝解档
     */
    @Override
    public void jjjd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待解档状态，不能操作");
        }
        mjyOld.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "拒绝解档");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝解档",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 同意借阅
     */
    @Override
    public void tyjy(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待借阅状态，不能操作");
        }
        mjyOld.setJyzt(JyztEnum.jyz.getValue());
        mjyOld.setJyksrq(form.getJyksrq());
        mjyOld.setJyjsrq(form.getJyjsrq());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "同意借阅", JcmcysEnum.green.getValue(), formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "同意借阅");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】同意借阅",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 拒绝借阅
     */
    @Override
    public void jjjy(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是待借阅状态，不能操作");
        }
        mjyOld.setJyzt(JyztEnum.wjy.getValue());
        mjyOld.setJyksrq(form.getJyksrq());
        mjyOld.setJyjsrq(form.getJyjsrq());
        this.updateById(mjyOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        formData.put("原因", form.getRemark());
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "拒绝借阅");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】拒绝借阅",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    /**
     * 确认归还
     */
    @Override
    public void qrgh(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Mjy mjyOld = this.getById(form.getId());
        if (mjyOld == null) {
            throw new ServiceException("麻精药不存在或已删除");
        }
        if (!mjyOld.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new ServiceException("麻精药【" + mjyOld.getMc() + "】不是借阅中状态，不能操作");
        }
        mjyOld.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(mjyOld);
        //稽查轨迹
        mjyJcgjService.saveJcgj(mjyOld.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjy, mjyOld.getId(), mjyOld.getMc(), mjyOld.getBh(), mjyOld.getNd()+mjyOld.getNddw(), "确认归还");
        //通知
        noticeService.save("麻精药【"+mjyOld.getMc()+"】确认归还",mjyOld.getGdsqrId(),getUrlQz("mjyList"));
    }

    //endregion

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




    /**
     * 获取通知的跳转url
     * @param tab
     * @return
     */
    private String getUrlQz(String tab){
        return  "/resource/mjy/"+tab;
    }

}
