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.gyzj.DetailDto;
import com.hxhq.business.dto.gyzj.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.zykgl.*;
import com.hxhq.business.form.gyzj.*;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.business.utils.lang.GyzjJcnrUtil;
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.GyzjMapper;
import com.hxhq.business.domain.Gyzj;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 给药制剂Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-18
 */
@Service
public class GyzjServiceImpl extends ServiceImpl<GyzjMapper, Gyzj> implements IGyzjService {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGyzjJcgjService gyzjJcgjService;
    @Autowired
    private IGyzjFfjlService gyzjFfjlService;
    @Autowired
    private IZcgService zcgService;
    @Autowired
    private IGyzjTzService gyzjTzService;
    @Autowired
    private IArchiveLogService archiveLogService;
    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IStudyGyzjService studyGyzjService;

    //region 资源库管理

    /**
     * 列表
     *
     * @param form 暂存柜
     * @return 暂存柜
     */
    @Override
    public List<ListDto> queryStudyList(SearchForm form) {
        QueryWrapper<Gyzj> 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.getStudyMc())) {
            queryWrapper.and(p -> p.like("s.`name`", form.getStudyMc()));
        }
        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<Gyzj> 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);
    }

    /**
     * 解档 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if(!gyzjOld.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】未归档，不能解档");
        }
        //申请解档后，制剂状态更新为待解档，等待档案员审核期间无法进行其他操作；
        gyzjOld.setZjzt(ZjztEnum.djd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "申请解档", JcmcysEnum.green.getValue(), null,  qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "申请解档");
    }

    /**
     * 借阅 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if(!gyzjOld.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是归档状态，不能借阅");
        }
        if (!gyzjOld.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是未借阅状态，不能借阅");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        gyzjOld.setJyzt(JyztEnum.djy.getValue());
        gyzjOld.setJyksrq(form.getStartDate());
        gyzjOld.setJyjsrq(form.getEndDate());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getStartDate()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getEndDate()));
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", JcmcysEnum.orange.getValue(),formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "申请借阅");
    }

    /**
     * 批量归档 
     *
     * @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<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        if (gyzjList.size() > 0) {
            for (Gyzj gyzj : gyzjList) {
                if (!gyzj.getZjzt().equals(ZjztEnum.ysd.getValue())) {
                    throw new ServiceException("给药制剂【" + gyzj.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (Gyzj gyzj : gyzjList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                gyzj.setZjzt(ZjztEnum.dgd.getValue());
                gyzj.setDazt(DaztEnum.ygd.getValue());
                gyzj.setGdsqrId(qmr.getUserId());
                gyzj.setGdsqrMc(qmr.getNickName());
                //稽查轨迹
                gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark()));
                //保存日志
                archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzj.getId(), gyzj.getMc(), gyzj.getBh(), gyzj.getNd()+gyzj.getNddw(), "申请归档");
            }
            this.updateBatchById(gyzjList);
            gyzjJcgjService.saveBatch(gyzjJcgjList);
        }
    }

    /**
     * 归档 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        gyzjOld.setZjzt(ZjztEnum.dgd.getValue());
        gyzjOld.setDazt(DaztEnum.ygd.getValue());
        gyzjOld.setGdsqrId(qmr.getUserId());
        gyzjOld.setGdsqrMc(qmr.getNickName());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "申请归档");
    }


    /**
     * 批量锁定 
     *
     * @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<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        if (gyzjList.size() <= 0) {
            throw new ServiceException("给药制剂不是入库状态，不能锁定");
        }
        for (Gyzj gyzj : gyzjList) {
            //锁定
            gyzj.setZjzt(ZjztEnum.ysd.getValue());
            //region 稽查轨迹
            gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "锁定给药制剂", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark()));
        }
        this.updateBatchById(gyzjList);
        gyzjJcgjService.saveBatch(gyzjJcgjList);

    }

    /**
     * 锁定 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库状态，不能锁定");
        }
        gyzjOld.setZjzt(ZjztEnum.ysd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "锁定给药制剂", JcmcysEnum.orange.getValue(),null, qmr, form.getRemark());
    }


    /**
     * 解锁 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        gyzjOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "解锁给药制剂", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
    }

    /**
     * 批量处置药剂 
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plczyj(PlczyjForm form) {
        SysUser clr = SecurityUtils.getLoginUser().getSysUser();
        //验证处理人密码
        checkPassword(clr,form.getClrmm(),false);

        QueryWrapper<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        if (gyzjList.size() <= 0) {
            throw new ServiceException("给药制剂不是入库状态，不能处置药剂");
        }
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        List<GyzjTz> gyzjTzList = new ArrayList<>();
        for (Gyzj gyzj : gyzjList) {
            List<PlczyjItem> gyzjPlffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(gyzj.getId()))
                    .collect(Collectors.toList());
            if (gyzjPlffItemFilter.size() > 0) {
                //region 验证
                if (StringUtils.isBlank(gyzjPlffItemFilter.get(0).getCzl())) {
                    throw new ServiceException("处置量不能为空");
                }
                BigDecimal czl = BigDecimal.ZERO;
                try {
                    czl = new BigDecimal(gyzjPlffItemFilter.get(0).getCzl());
                } catch (Exception ex) {
                    throw new ServiceException("处置量填写不正确");
                }
                //验证库存
                BigDecimal kc = new BigDecimal(gyzj.getKc());

                gyzj.setKc(kc.subtract(czl).toString());
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("处置方式", gyzjPlffItemFilter.get(0).getCzfs());
                formData.put("处置原因", gyzjPlffItemFilter.get(0).getCzyy());
                formData.put("处置量", gyzjPlffItemFilter.get(0).getCzl() + gyzj.getKcdw());
                formData.put("处置人", clr.getNickName());
                gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "处置药剂", JcmcysEnum.orange.getValue(),formData, clr, null));
                //台账
                gyzjTzList.add(gyzjTzService.getTz(gyzj.getId(),null,null,null,null,null,null,null,null,clr,"处置药剂",gyzjPlffItemFilter.get(0).getCzl(),gyzj.getKcdw(),gyzjPlffItemFilter.get(0).getCzyy()));

            } else {
                throw new ServiceException("给药制剂【" + gyzj.getMc() + "】不存在或已删除");
            }
        }
        this.updateBatchById(gyzjList);
        gyzjJcgjService.saveBatch(gyzjJcgjList);
        gyzjTzService.saveBatch(gyzjTzList);
    }


    /**
     * 处置药剂 
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void czyj(CzyjForm form) {
        SysUser clr = SecurityUtils.getLoginUser().getSysUser();

        //验证处理人密码
        checkPassword(clr,form.getClrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库状态，不能处置药剂");
        }
        if (StringUtils.isBlank(form.getCzl())) {
            throw new ServiceException("处置量不能为空");
        }
        BigDecimal czl = BigDecimal.ZERO;
        try {
            czl = new BigDecimal(form.getCzl());
        } catch (Exception ex) {
            throw new ServiceException("处置量填写不正确");
        }
        //验证库存
        BigDecimal kc = new BigDecimal(gyzjOld.getKc());
        if (kc.compareTo(BigDecimal.ZERO) <= 0) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】库存不足，不能处置");
        }
        if (kc.compareTo(czl) < 0) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】处置量大于库存量");
        }
        //更新库存
        gyzjOld.setKc(kc.subtract(czl).toString());
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("处置方式", form.getCzfs());
        formData.put("处置原因", form.getCzyy());
        formData.put("处置量", form.getCzl() + gyzjOld.getKcdw());
        formData.put("处置人", clr.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "处置药剂", JcmcysEnum.green.getValue(), formData, clr, null);
        //台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,null,null,null,null,null,null,clr,"处置药剂",form.getCzl(),gyzjOld.getKcdw(),form.getCzyy());
        this.updateById(gyzjOld);
    }


    /**
     * 批量处置容器 
     *
     * @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<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        queryWrapper.eq("rqzt", RqztEnum.wcz.getValue());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        List<GyzjTz> gyzjTzList = new ArrayList<>();

        if (gyzjList.size() <= 0) {
            throw new ServiceException("给药制剂已经处置过容器了，不能重复操作");
        }
        for (Gyzj gyzj : gyzjList) {
            List<PlczrqItem> gyzjPlffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(gyzj.getId()))
                    .collect(Collectors.toList());
            if (gyzjPlffItemFilter.size() > 0) {
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("处置方式", gyzjPlffItemFilter.get(0).getCzfs());
                formData.put("处置原因", gyzjPlffItemFilter.get(0).getCzyy());
                formData.put("处置人", clr.getNickName());
                formData.put("复核人", fhr.getNickName());
                formData.put("监督人", jdr.getNickName());
                gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "处置容器", JcmcysEnum.green.getValue(), formData, clr, null));
                //台账
                gyzjTzList.add(gyzjTzService.getTz(gyzj.getId(),null,null,null,null,null,null,null,null,clr,"处置容器",null,null,gyzjPlffItemFilter.get(0).getCzyy()));
                gyzj.setRqzt(RqztEnum.ycz.getValue());
            } else {
                throw new ServiceException("给药制剂【" + gyzj.getMc() + "】不存在或已删除");
            }
        }
        this.updateBatchById(gyzjList);
        gyzjJcgjService.saveBatch(gyzjJcgjList);
        gyzjTzService.saveBatch(gyzjTzList);
    }

    /**
     * 处置容器 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库状态，不能处置容器");
        }
        if (!gyzjOld.getRqzt().equals(RqztEnum.wcz.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】已经处置过容器了，不能重复操作");
        }
        //稽查轨迹
        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());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "处置容器", JcmcysEnum.green.getValue(), formData,  clr,null);
        //台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,null,null,null,null,null,null,clr,"处置容器",null,null,form.getCzyy());
        gyzjOld.setRqzt(RqztEnum.ycz.getValue());
        this.updateById(gyzjOld);
    }


    /**
     * 钥匙发放 
     *
     * @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<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        if (gyzjList.size() <= 0) {
            throw new ServiceException("药剂已被发放,请不要重复发放");
        }
        QueryWrapper<GyzjFfjl> gyzjFfjlQueryWrapper = Wrappers.query();
        gyzjFfjlQueryWrapper.in("gyzj_id", form.getIds());
        List<GyzjFfjl> gyzjFfjlList = gyzjFfjlService.list(gyzjFfjlQueryWrapper);
        for (GyzjFfjl gyzjFfjl : gyzjFfjlList) {
            gyzjFfjl.setZcgId(zcg.getId());
        }
        String glyj = "";
        for (Gyzj gyzj : gyzjList) {
            //更新暂存柜id
            gyzj.setZcgId(zcg.getId());
            //更新暂存柜关联药剂
            if (StringUtils.isBlank(glyj)) {
                glyj += gyzj.getMc();
            } else {
                glyj += "," + gyzj.getMc();
            }
            //稽查轨迹
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("钥匙1领取人", lqr1.getNickName());
            formData.put("钥匙2领取人", lqr2.getNickName());
            formData.put("发放人", ffr1.getNickName());
            gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.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(gyzjList);
        gyzjJcgjService.saveBatch(gyzjJcgjList);
        if(gyzjFfjlList.size()>0){
            gyzjFfjlService.updateBatchById(gyzjFfjlList);
        }
    }


    /**
     * 发放 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库状态，不能发放");
        }
        Date now = new Date();

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

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

        //region 更新发放信息
        gyzjOld.setCkl(gyzjOld.getKc());
        gyzjOld.setCkldw(gyzjOld.getKcdw());
        gyzjOld.setFfrq(now);
        gyzjOld.setFfbz(form.getRemark());
        gyzjOld.setMdIds(form.getMdIds());
        gyzjOld.setMdOther(form.getMdOther());
        gyzjOld.setZjzt(ZjztEnum.yff.getValue());
        //endregion

        this.updateById(gyzjOld);
    }


    /**
     * 批量发放 
     *
     * @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<Gyzj> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Gyzj> gyzjList = this.list(queryWrapper);
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        List<GyzjTz> gyzjTzList = new ArrayList<>();
        if (gyzjList.size() <= 0) {
            throw new ServiceException("给药制剂不是入库状态，不能发放");
        }
        Date now = new Date();
        for (Gyzj gyzj : gyzjList) {
            //验证库存
            BigDecimal kc = new BigDecimal(gyzj.getKc());
            if (kc.compareTo(BigDecimal.ZERO) <= 0) {
                throw new ServiceException("给药制剂【" + gyzj.getMc() + "】库存不足，不能发放");
            }
            List<FfForm> plffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(gyzj.getId()))
                    .collect(Collectors.toList());
            if (plffItemFilter.size() > 0) {
                //发放记录，试验物资
                gyzjFfjlService.ff(plffItemFilter.get(0), gyzj, lqr1, lqr2, ffr1, ffr2, now);
                //稽查轨迹
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("出库量", gyzj.getKc() + gyzj.getKcdw());
                formData.put("转移条件", plffItemFilter.get(0).getZytj());
                formData.put("出库毛重", plffItemFilter.get(0).getCkmz() + gyzj.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());
                gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "领取发放", JcmcysEnum.green.getValue(), formData,  ffr1,plffItemFilter.get(0).getRemark()));
                //台账
                gyzjTzList.add(gyzjTzService.getTz(gyzj.getId(),lqr1,lqr2,null,null,ffr1,ffr2,null,null,null,"领取发放",gyzj.getKc(),gyzj.getKcdw(),plffItemFilter.get(0).getRemark()));
                //region 更新发放信息
                gyzj.setCkl(gyzj.getKc());
                gyzj.setCkldw(gyzj.getKcdw());
                gyzj.setFfrq(now);
                gyzj.setFfbz(plffItemFilter.get(0).getRemark());
                gyzj.setMdIds(plffItemFilter.get(0).getMdIds());
                gyzj.setMdOther(plffItemFilter.get(0).getMdOther());
                gyzj.setZjzt(ZjztEnum.yff.getValue());
                //endregion

            } else {
                throw new ServiceException("给药制剂【" + gyzj.getMc() + "】不存在或已删除");
            }
        }
        gyzjJcgjService.saveBatch(gyzjJcgjList);
        gyzjTzService.saveBatch(gyzjTzList);
        this.saveOrUpdateBatch(gyzjList);
    }

    /**
     * 编辑 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getBjzt().equals(BjEnum.wbj.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】已经提交编辑申请，请等待审核");
        }
        if (!(gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库或已发放状态，不能编辑");
        }
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();

        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(gyzjOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "申请编辑", JcmcysEnum.green.getValue(), fieldChange.toString(), fieldChange.toEnString(),  qmr,null));
        }
        gyzjOld.setMcbj(form.getMc());
        gyzjOld.setNdbj(form.getNd());
        gyzjOld.setNddwbj(form.getNddw());
        gyzjOld.setSxrqbj(form.getSxrq());
        gyzjOld.setCctjbj(form.getCctj());
        gyzjOld.setCcwzbj(form.getCcwz());

        gyzjOld.setBjbz(form.getBjbz());
        gyzjOld.setBjrId(SecurityUtils.getUserId());
        gyzjOld.setBjrMc(SecurityUtils.getNickName());
        gyzjOld.setBjzt(BjEnum.shz.getValue());
        this.updateById(gyzjOld);
        gyzjJcgjService.saveBatch(gyzjJcgjList);
    }

    /**
     * 审核编辑 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getBjzt().equals(BjEnum.shz.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】没有提交编辑申请，不能审核编辑");
        }
        if (!(gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库或已发放状态，不能审核编辑");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", gyzjOld.getBjbz());
        formData.put("审核备注", form.getRemark());
        if (form.getQmyy().equals(ShbjEnum.ty.getValue())) {
            gyzjOld.setCcwz(gyzjOld.getCcwzbj());
            gyzjOld.setCctj(gyzjOld.getCctjbj());
            gyzjOld.setSxrq(gyzjOld.getSxrqbj());
            gyzjOld.setNddw(gyzjOld.getNddwbj());
            gyzjOld.setNd(gyzjOld.getNdbj());
            gyzjOld.setMc(gyzjOld.getMcbj());
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意编辑", JcmcysEnum.green.getValue(), formData,qmr,null);
            //通知
            noticeService.save("给药制剂【"+gyzjOld.getMc()+"】同意编辑",gyzjOld.getBjrId(),getUrlQz("gyzjList"));
        } else {
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝编辑", JcmcysEnum.red.getValue(), formData, qmr,null);
            //通知
            noticeService.save("给药制剂【"+gyzjOld.getMc()+"】拒绝编辑",gyzjOld.getBjrId(),getUrlQz("gyzjList"));
        }
        //清空编辑信息
        gyzjOld.setMcbj(null);
        gyzjOld.setNddwbj(null);
        gyzjOld.setNdbj(null);
        gyzjOld.setSxrqbj(null);
        gyzjOld.setCctjbj(null);
        gyzjOld.setCcwzbj(null);
        gyzjOld.setBjbz(null);
        gyzjOld.setBjrId(null);
        gyzjOld.setBjrMc(null);
        gyzjOld.setBjzt(BjEnum.wbj.getValue());
        this.updateById(gyzjOld);
        //endregion
    }


    /**
     * 修改库存 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getKcbjzt().equals(KcbjEnum.wbj.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】已经提交修改库存申请，请等待审核");
        }
        if (!(gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库或已发放状态，不能修改库存");
        }
        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjectsNotNULL(gyzjOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }
        List<GyzjJcgj> gyzjJcgjList = new ArrayList<>();
        
        //region 稽查轨迹
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            gyzjJcgjList.add(gyzjJcgjService.getJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "修改库存申请", JcmcysEnum.green.getValue(), fieldChange.toString(), fieldChange.toEnString(),  qmr,null));
        }
        //endregion

        gyzjOld.setKcdwbj(form.getKcdw());
        gyzjOld.setKcbj(form.getKc());
        gyzjOld.setKcbjrId(SecurityUtils.getUserId());
        gyzjOld.setKcbjrMc(SecurityUtils.getNickName());
        gyzjOld.setKcbjzt(KcbjEnum.shz.getValue());
        gyzjOld.setKcbjbz(form.getKcbjbz());
        this.updateById(gyzjOld);
        gyzjJcgjService.saveBatch(gyzjJcgjList);

    }


    /**
     * 审核修改库存 
     *
     * @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getKcbjzt().equals(KcbjEnum.shz.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】没有提交修改库存申请，不能审核修改库存");
        }
        if (!(gyzjOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库或已发放状态，不能审核修改库存");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", gyzjOld.getKcbjbz());
        formData.put("审核备注", form.getRemark());
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意修改库存", JcmcysEnum.green.getValue(), formData, qmr,null);
            gyzjOld.setKcdw(gyzjOld.getKcdwbj());
            gyzjOld.setKc(gyzjOld.getKcbj());
            //通知
            noticeService.save("给药制剂【"+gyzjOld.getMc()+"】同意修改库存",gyzjOld.getKcbjrId(),getUrlQz("gyzjList"));
        } else {
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝修改库存", JcmcysEnum.red.getValue(), formData,  qmr,null);
            //通知
            noticeService.save("给药制剂【"+gyzjOld.getMc()+"】拒绝修改库存",gyzjOld.getKcbjrId(),getUrlQz("gyzjList"));
        }
        //台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,null,null,null,null,null,null,qmr,"修改库存",gyzjOld.getKcbj(),gyzjOld.getKcdw(),gyzjOld.getKcbjbz());

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


    /**
     * 申请归还 
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sqgh(GhForm form) {
        SysUser ghr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser ghr2 = sysUserService.selectUserById(form.getGhr2Id());
        //验证归还人1密码
        checkPassword(ghr1,form.getGhr1mm(),true);
        //验证归还人2密码
        checkPassword(ghr2,form.getGhr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已发放状态，不能归还");
        }
        if (!gyzjOld.getGhzt().equals(GhztEnum.wsq.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是未申请归还状态，不能归还");
        }
        if (!gyzjOld.getCczt().equals(CcztEnum.Qc.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】是暂存状态，请先将该物质从暂存处取出");
        }
        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getGhzytj());
        formData.put("归还人1", SecurityUtils.getNickName());
        formData.put("归还人2", ghr2.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "归还", JcmcysEnum.green.getValue(), formData, ghr1, form.getRemark());
        //endregion

        gyzjOld.setGhzytj(form.getGhzytj());
        gyzjOld.setGhzytj(form.getGhzytj());
        gyzjOld.setGhzytj(form.getGhzytj());

        gyzjOld.setGhzytj(form.getGhzytj());
        gyzjOld.setRkwz(form.getRkwz());
        gyzjOld.setRktj(form.getRktj());
        gyzjOld.setGhr1Id(SecurityUtils.getUserId());
        gyzjOld.setGhr1Mc(SecurityUtils.getNickName());
        gyzjOld.setGhr2Id(ghr2.getUserId());
        gyzjOld.setGhr2Mc(ghr2.getNickName());
        gyzjOld.setGhzt(GhztEnum.sqgh.getValue());
        this.updateById(gyzjOld);
    }

    /**
     * 确认归还 
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gh(GhForm form) {
        SysUser jsr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser jsr2 = sysUserService.selectUserById(form.getJsr2Id());
        //验证接收人1密码
        checkPassword(jsr1,form.getJsr1mm(),true);
        //验证接收人2密码
        checkPassword(jsr2,form.getJsr2mm(),true);
        
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        SysUser ghr1 = sysUserService.selectUserById(gyzjOld.getGhr1Id());
        SysUser ghr2 = sysUserService.selectUserById(gyzjOld.getGhr2Id());
        if (!gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已发放状态，不能归还");
        }
        if (!gyzjOld.getGhzt().equals(GhztEnum.sqgh.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是申请归还状态，不能归还");
        }
        if (!gyzjOld.getCczt().equals(CcztEnum.Qc.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】是暂存状态，请先将该物质从暂存处取出");
        }
        gyzjFfjlService.gh(form, gyzjOld, ghr1, ghr2, jsr1, jsr2);
        // 台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,ghr1,ghr2,null,null,jsr1,jsr2,null,"归还",form.getRkl(), gyzjOld.getKcdw(),form.getRemark());

        // 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getGhzytj());
        formData.put("入库净重", form.getRkl() + gyzjOld.getKcdw());
        formData.put("入库毛重", form.getRkmz() + gyzjOld.getKcdw());
        formData.put("库管员1", jsr1.getNickName());
        formData.put("库管员2", jsr2.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), formData, jsr1, SecurityUtils.getNickName());
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】确认归还",ghr1.getUserId(),getUrlQz("gyzjList"));
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】确认归还",ghr2.getUserId(),getUrlQz("gyzjList"));
        // 更新
        gyzjOld.setKc(form.getRkl());
        gyzjOld.setGhzytj(null);
        gyzjOld.setRkwz(null);
        gyzjOld.setRktj(null);
        gyzjOld.setZjzt(ZjztEnum.rk.getValue());
        gyzjOld.setGhzt(GhztEnum.wsq.getValue());
        this.updateById(gyzjOld);
    }


    /**
     * 申请入库 
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sqrk(RkForm form) {
        SysUser qmr1 = SecurityUtils.getLoginUser().getSysUser();
        SysUser qmr2 = sysUserService.selectUserById(form.getQmr2Id());
        //验证签名人1密码
        checkPassword(qmr1,form.getQmr1mm(),true);
        //验证签名人2密码
        checkPassword(qmr2,form.getQmr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.wrk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已发放状态，不能归还");
        }
        if (!gyzjOld.getGhzt().equals(GhztEnum.wsq.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是未申请归还状态，不能归还");
        }
        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getRkzytj());
        formData.put("签名人1", qmr1.getNickName());
        formData.put("签名人2", qmr2.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "给药制剂入库", JcmcysEnum.green.getValue(), formData, qmr1, form.getRemark());
        //endregion

        gyzjOld.setRkzytj(form.getRkzytj());
        gyzjOld.setRkwz(form.getRkwz());
        gyzjOld.setRkbz(form.getRemark());
        gyzjOld.setRktj(form.getRktj());
        gyzjOld.setQmr1Id(qmr1.getUserId());
        gyzjOld.setQmr1Mc(qmr1.getNickName());
        gyzjOld.setQmr2Id(qmr2.getUserId());
        gyzjOld.setQmr2Mc(qmr2.getNickName());
        gyzjOld.setRksqzt(RksqztEnum.sqrk.getValue());
        this.updateById(gyzjOld);
    }

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

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.wrk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是未入库状态，不能归还");
        }
        if (!gyzjOld.getRksqzt().equals(RksqztEnum.sqrk.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是入库申请状态，不能归还");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", gyzjOld.getRkbz());
        formData.put("审核备注", form.getRemark());
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            SysUser qmr1 = sysUserService.selectUserById(form.getQmr1Id());
            SysUser qmr2 = sysUserService.selectUserById(form.getQmr2Id());
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意入库", JcmcysEnum.green.getValue(), formData,  jsr1,null);
            gyzjOld.setKc(form.getRkl());
            gyzjOld.setZjzt(ZjztEnum.rk.getValue());
            //台账
            gyzjTzService.saveTz(gyzjOld.getId(),null,null,qmr1,qmr2,null,null,jsr1,jsr2,null,"给药制剂入库",form.getRkl(),gyzjOld.getKcdw(),form.getRemark());
        } else {
            gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝入库", JcmcysEnum.red.getValue(), formData,  jsr1,null);
        }
        // 更新
        gyzjOld.setRkzytj(null);
        gyzjOld.setRkwz(null);
        gyzjOld.setRktj(null);
        gyzjOld.setQmr1Id(null);
        gyzjOld.setQmr1Mc(null);
        gyzjOld.setQmr2Id(null);
        gyzjOld.setQmr2Mc(null);
        gyzjOld.setRksqzt(RksqztEnum.wsq.getValue());
        this.updateById(gyzjOld);
    }


    /**
     * 存储
     *
     * @param form
     */
    @Override
    public void cc(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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已发放状态，不能解锁");
        }
        gyzjOld.setCcwz(form.getCcwz());
        gyzjOld.setCctj(form.getCctj());
        gyzjOld.setCczt(CcztEnum.Cc.getValue());
        gyzjOld.setCczt(CcztEnum.Cc.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("存储位置", form.getCcwz());
        formData.put("存储条件", form.getCctj());
        formData.put("签名人1", qmr.getNickName());
        formData.put("签名人2", qmr.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "存储", JcmcysEnum.green.getValue(), formData, null, form.getRemark());
        //台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,qmr,qmr2,null,null,null,null,null,"存储",gyzjOld.getKc(),gyzjOld.getKcdw(),form.getRemark());

    }

    /**
     * 取出
     *
     * @param form
     */
    @Override
    public void qc(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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是已发放状态，不能解锁");
        }
        gyzjOld.setCczt(CcztEnum.Qc.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("签名人1", qmr.getNickName());
        formData.put("签名人2", qmr.getNickName());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "取出", JcmcysEnum.green.getValue(), formData, null, form.getRemark());
        //台账
        gyzjTzService.saveTz(gyzjOld.getId(),null,null,qmr,qmr2,null,null,null,null,null,"取出",gyzjOld.getKc(),gyzjOld.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, Gyzj> gyzjMap = new HashMap<>();
        List<Gyzj> gyzjList = new ArrayList<>();
        LinkedList<GyzjJcgj> jcgjList = new LinkedList<>();
        LinkedList<GyzjTz> 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<Gyzj> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.in(Gyzj::getBh, bhList);
            gyzjList = this.list(queryWrapper);
            for (Gyzj gyzj : gyzjList) {
                gyzjMap.put(gyzj.getBh(), gyzj);
            }

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

        // 存储
        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()) {
                    Gyzj gyzj = gyzjMap.get(bh);
                    // 稽查轨迹
                    HashMap<String, String> formData = new HashMap<>();
                    formData.put("存储位置", studyFormFillCc.getCcwz());
                    formData.put("存储条件", studyFormFillCc.getCctj());
                    jcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "存储",JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(GyzjJcnrUtil.getMapEn(formData)), qmr,remark));
                    // 台账
                    tzList.add(gyzjTzService.getTzGen(gyzj.getId(), null,null, null,null, null,null, null,null, qmr,"存储" ,gyzj.getKc(),gyzj.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()) {
                    Gyzj gyzj = gyzjMap.get(bh);
                    // 稽查轨迹
                    HashMap<String, String> formData = new HashMap<>();
                    formData.put("处置方式", studyFormFillCz.getCzfs());
                    formData.put("处置量", gyzj.getKc() + gyzj.getKcdw());
                    jcgjList.add(gyzjJcgjService.getJcgj(gyzj.getId(), JcgjlxEnum.lc.getValue(), "处置",JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(GyzjJcnrUtil.getMapEn(formData)), qmr,remark));

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

                }
                baseMapper.updateKcBatch(studyFormFillCz.getList(), "0");
            }
        }
        gyzjJcgjService.saveBatch(jcgjList);
        gyzjTzService.saveBatch(tzList);
        studyGyzjService.saveBatch(studyId, gyzjList);
    }

    //endregion

    //region 档案管理

    /**
     * 查询给药制剂列表
     *
     * @param form 给药制剂
     * @return 给药制剂集合
     */
    @Override
    public List<ListDto> queryArchiveList(SearchForm form){
        QueryWrapper<Gyzj> 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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待归档状态，不能操作");
        }
        gyzjOld.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "同意归档");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】同意归档",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    /**
     * 拒绝归档
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待归档状态，不能操作");
        }
        gyzjOld.setZjzt(ZjztEnum.ysd.getValue());
        gyzjOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "拒绝归档");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】拒绝归档",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    /**
     * 同意解档
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待解档状态，不能操作");
        }
        gyzjOld.setZjzt(ZjztEnum.ysd.getValue());
        gyzjOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "同意解档");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】同意解档",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    /**
     * 拒绝解档
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待解档状态，不能操作");
        }
        gyzjOld.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "拒绝解档");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】拒绝解档",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));

    }

    /**
     * 同意借阅
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待借阅状态，不能操作");
        }
        gyzjOld.setJyzt(JyztEnum.jyz.getValue());
        gyzjOld.setJyksrq(form.getJyksrq());
        gyzjOld.setJyjsrq(form.getJyjsrq());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "同意借阅", JcmcysEnum.green.getValue(), formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "同意借阅");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】同意借阅",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    /**
     * 拒绝借阅
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是待借阅状态，不能操作");
        }
        gyzjOld.setJyzt(JyztEnum.wjy.getValue());
        gyzjOld.setJyksrq(form.getJyksrq());
        gyzjOld.setJyjsrq(form.getJyjsrq());
        this.updateById(gyzjOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        formData.put("原因", form.getRemark());
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "拒绝借阅");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】拒绝借阅",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    /**
     * 确认归还
     */
    @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不正确");
        }
        Gyzj gyzjOld = this.getById(form.getId());
        if (gyzjOld == null) {
            throw new ServiceException("给药制剂不存在或已删除");
        }
        if (!gyzjOld.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new ServiceException("给药制剂【" + gyzjOld.getMc() + "】不是借阅中状态，不能操作");
        }
        gyzjOld.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gyzjOld);
        //稽查轨迹
        gyzjJcgjService.saveJcgj(gyzjOld.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzj, gyzjOld.getId(), gyzjOld.getMc(), gyzjOld.getBh(), gyzjOld.getNd()+gyzjOld.getNddw(), "确认归还");
        //通知
        noticeService.save("给药制剂【"+gyzjOld.getMc()+"】确认归还",gyzjOld.getGdsqrId(),getUrlQz("gyzjList"));
    }

    //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/gyzj/"+tab;
    }


}
