package com.hxhq.business.service.impl;

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

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.gsp.GspDto;
import com.hxhq.business.dto.gsp.GspListDto;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.form.gsp.*;
import com.hxhq.business.service.*;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.ObjectCompareUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.common.core.utils.DateUtils;
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.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.GspMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供试品管理Service业务层处理
 *
 * @author HanLong
 * @date 2025-12-30
 */
@Service
public class GspServiceImpl extends ServiceImpl<GspMapper, Gsp> implements IGspService {
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IGspJcgjService gspJcgjService;

    @Autowired
    private IGspTzService gspTzService;

    @Autowired
    private IGspFfjlService gspFfjlService;

    @Autowired
    private IZcgService zcgService;

    @Autowired
    private IArchiveLogService archiveLogService;

    @Autowired
    private INoticeService noticeService;
    /**
     * 查询供试品管理列表
     *
     * @param form 供试品列表参数
     * @return 供试品管理
     */
    @Override
    public List<Gsp> queryList(GspSearchListForm form) {
        QueryWrapper<Gsp> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(form.getMc())) {
            queryWrapper.like("mc", form.getMc());
        }
        if(StringUtils.isNotEmpty(form.getBh())) {
            queryWrapper.like("bh", form.getBh());
        }
        if(StringUtils.isNotEmpty(form.getPh())) {
            queryWrapper.like("ph", form.getPh());
        }
        if(StringUtils.isNotEmpty(form.getGg())) {
            queryWrapper.and(p -> p.apply("CONCAT(gg, ggdw) LIKE #{0}", form.getGg()));
        }
        if(StringUtils.isNotEmpty(form.getStartDate())) {
            queryWrapper.ge("yxq", form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le("yxq", form.getEndDate());
        }
        if(form.getJyzt() != null) {
            queryWrapper.eq("jyzt", form.getJyzt());
        }
        if(form.getZjzt() != null) {
            queryWrapper.eq("zjzt", form.getZjzt());
        }
        if(form.getStudyId() != null) {
            queryWrapper.and(p -> p.apply("find_in_set(md_ids, {0})", form.getStudyId()));
        }
        if(form.getArchive() != null && form.getArchive() == DaztEnum.ygd.getValue()) {
            queryWrapper.in("zjzt", ZjztEnum.dgd.getValue(), ZjztEnum.gd.getValue(), ZjztEnum.djd.getValue());
        }
        return this.list(queryWrapper);
    }

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

    @Override
    public void save(GspForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        Gsp gsp = new Gsp();
        BeanUtils.copyProperties(form, gsp);
        gsp.setZjzt(ZjztEnum.rk.getValue());
        gsp.setJyzt(JyztEnum.wjy.getValue());
        gsp.setBjzt(BjEnum.wbj.getValue());
        gsp.setKcbjzt(KcbjEnum.wbj.getValue());
        save(gsp);


        GspJcgj gspJcgj = new GspJcgj();
        gspJcgj.setGspId(gsp.getId());
        gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
        gspJcgj.setJcmc("新增供试品");
        gspJcgj.setJcmcEn("Add Test");
        gspJcgj.setJcmcys(JcmcysEnum.blue.getValue());
        gspJcgj.setRemark(form.getQmbz());
        gspJcgj.setQmrId(qmr.getUserId());
        gspJcgj.setQmrMc(qmr.getNickName());
        gspJcgjService.save(gspJcgj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(GspBjForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        Gsp gspOld = this.getById(form.getId());
        if(gspOld == null) {
            throw new ServiceException("供试品不存在");
        } else if(gspOld.getZjzt() != ZjztEnum.rk.getValue()) {
            throw new ServiceException("供试品【" + gspOld.getZjzt() + "】不是入库状态，不能编辑");
        } else if (!gspOld.getBjzt().equals(BjEnum.wbj.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】已经提交编辑申请，请等待审核");
        }

        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjects(gspOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }

        gspOld.setMcbj(form.getMc());
        gspOld.setPhbj(form.getPh());
        gspOld.setGgbj(form.getGg());
        gspOld.setGgdwbj(form.getGgdw());
        gspOld.setYxqbj(form.getYxq());
        gspOld.setJsrqbj(form.getJsrq());
        gspOld.setCctjbj(form.getCctj());
        gspOld.setZysxbj(form.getZysx());
        gspOld.setBjbz(form.getBjbz());
        gspOld.setBjzt(BjEnum.shz.getValue());
        gspOld.setBjrId(qmr.getUserId());
        gspOld.setBjrMc(qmr.getNickName());
        updateById(gspOld);


        List<GspJcgj> gspJcgjList = new ArrayList<>();
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            GspJcgj gspJcgj = new GspJcgj();
            gspJcgj.setGspId(gspOld.getId());
            gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspJcgj.setJcmc("申请编辑");
            gspJcgj.setJcmcEn("Apply for Edit");
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            gspJcgj.setJcnr(fieldChange.toString());
            gspJcgj.setJcnrEn(fieldChange.toEnString());
            gspJcgj.setQmrId(qmr.getUserId());
            gspJcgj.setQmrMc(qmr.getNickName());
            gspJcgj.setQmrMcEn(qmr.getUserName());
            gspJcgj.setRemark(form.getBjbz());
            gspJcgjList.add(gspJcgj);
        }
        gspJcgjService.saveBatch(gspJcgjList);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shbj(GspShbjForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getBjzt().equals(BjEnum.shz.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】没有提交编辑申请，不能审核编辑");
        }
        if (!(gspOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gspOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是入库或已发放状态，不能审核编辑");
        }
        //region 稽查轨迹
        GspJcgj gspJcgj = new GspJcgj();
        gspJcgj.setGspId(gspOld.getId());
        gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
        gspJcgj.setJcmc(ShbjEnum.getEnumByValue(form.getQmyy()).getText());
        if (form.getQmyy().equals(ShbjEnum.ty.getValue())) {
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            noticeService.save("供试品【"+gspOld.getMc()+"】同意编辑",gspOld.getBjrId(),getUrlQz("gspList"));
        } else {
            gspJcgj.setJcmcys(JcmcysEnum.red.getValue());
            noticeService.save("供试品【"+gspOld.getMc()+"】拒绝编辑",gspOld.getBjrId(),getUrlQz("gspList"));
        }
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", gspOld.getBjbz());
        formData.put("审核备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Application Comment", gspOld.getBjbz());
        formDataEn.put("Approve Comment", form.getRemark());
        gspJcgj.setJcnr(JctUtil.formatStr(formData));
        gspJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
        gspJcgj.setQmrId(qmr.getUserId());
        gspJcgj.setQmrMc(qmr.getNickName());
        gspJcgj.setQmrMcEn(qmr.getUserName());
        gspJcgjService.save(gspJcgj);
        //endregion

        //region 更新
        if (form.getQmyy().equals(ShbjEnum.ty.getValue())) {
            gspOld.setMc(gspOld.getMcbj());
            gspOld.setPh(gspOld.getPhbj());
            gspOld.setGg(gspOld.getGgbj());
            gspOld.setGgdw(gspOld.getGgdwbj());
            gspOld.setYxq(gspOld.getYxqbj());
            gspOld.setJsrq(gspOld.getJsrqbj());
            gspOld.setCctj(gspOld.getCctjbj());
            gspOld.setZysx(gspOld.getZysxbj());
        }

        //清空编辑信息
        gspOld.setMcbj(null);
        gspOld.setPhbj(null);
        gspOld.setGgbj(null);
        gspOld.setGgdwbj(null);
        gspOld.setYxqbj(null);
        gspOld.setJsrqbj(null);
        gspOld.setCctjbj(null);
        gspOld.setZysxbj(null);
        gspOld.setBjzt(BjEnum.wbj.getValue());
        this.updateById(gspOld);
        //endregion
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ysff(GspYsffForm form) {
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        SysUser lqr2 = sysUserService.selectUserById(form.getLqr2Id());
        SysUser ffr1 = sysUserService.selectUserById(form.getFfrId());
        sysUserService.checkPassword(lqr1, form.getLqr1mm(), true);
        sysUserService.checkPassword(lqr2, form.getLqr2mm(), true);
        sysUserService.checkPassword(ffr1, form.getFfrmm(), true);

        Zcg zcg = zcgService.getById(form.getZcgId());
        if (zcg == null) {
            throw new ServiceException("暂存柜不存在或已删除");
        }
        QueryWrapper<Gsp> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.isNull("zcg_id");
        List<Gsp> gspList = this.list(queryWrapper);
        List<GspJcgj> gspJcgjList = new ArrayList<>();
        if (gspList.size() <= form.getIds().length) {
            throw new ServiceException("药剂已被发放,请不要重复发放");
        }
        String glyj = "";
        for (Gsp gsp : gspList) {
            //更新暂存柜id
            gsp.setZcgId(zcg.getId());
            //更新暂存柜关联药剂
            if (com.hxhq.common.core.utils.StringUtils.isBlank(glyj)) {
                glyj += gsp.getMc();
            } else {
                glyj += "," + gsp.getMc();
            }

            //region 稽查轨迹
            GspJcgj gspJcgj = new GspJcgj();
            gspJcgj.setGspId(gsp.getId());
            gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspJcgj.setJcmc("钥匙发放");
            gspJcgj.setJcmcEn("Key Distribution");
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());

            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getRemark());
            formData.put("钥匙1领取人", lqr1.getNickName());
            if(lqr2 != null) {
                formData.put("钥匙2领取人", lqr2.getNickName());
            }
            formData.put("发放人", ffr1.getNickName());

            Map<String, String> formDataEn = new LinkedHashMap<>();
            formDataEn.put("Comment", form.getRemark());
            formDataEn.put("Key Recipient", lqr1.getUserName());
            if(lqr2 != null) {
                formDataEn.put("Key Recipient 2", lqr2.getUserName());
            }
            formDataEn.put("Issuer", ffr1.getUserName());

            gspJcgj.setJcnr(JctUtil.formatStr(formData));
            gspJcgj.setQmrId(SecurityUtils.getUserId());
            gspJcgj.setQmrMc(SecurityUtils.getNickName());
            gspJcgj.setQmrMcEn(SecurityUtils.getUsername());
            gspJcgjList.add(gspJcgj);
            //endregion
        }
        if (com.hxhq.common.core.utils.StringUtils.isBlank(zcg.getGlyj())) {
            zcg.setGlyj(glyj);
        } else {
            zcg.setGlyj(zcg.getGlyj() + "," + glyj);
        }
        zcg.setZt(zcgJyztEnum.jyz.getValue());
        zcgService.updateById(zcg);
        this.saveOrUpdateBatch(gspList);
        gspJcgjService.saveBatch(gspJcgjList);
    }

    @Override
    public void ff(FfGspForm form) {
        //验证领取人1、领取人2、发放人1、发放人2密码
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        sysUserService.checkPassword(lqr1, form.getLqr1mm(), true);
        SysUser lqr2 = null;
        if(form.getLqr2Id() != null) {
            lqr2 = sysUserService.selectUserById(form.getLqr2Id());
            sysUserService.checkPassword(lqr2, form.getLqr2mm(), true);
        }
        SysUser ffr1 = sysUserService.selectUserById(form.getFfr1Id());
        sysUserService.checkPassword(ffr1, form.getFfr1mm(), true);
        SysUser ffr2 = null;
        if(form.getFfr2Id() != null) {
            ffr2 = sysUserService.selectUserById(form.getFfr2Id());
            sysUserService.checkPassword(ffr2, form.getFfr2mm(), true);
        }

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

        Date now = new Date();
        gspFfjlService.ff(gspOld.getKc(), gspOld.getKcdw(), form.getZytj(),form.getRemark(),form.getMdIds(),gspOld,lqr1,lqr2,ffr1,ffr2,now);
        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("出库量", gspOld.getKc()+ gspOld.getKcdw());
        formData.put("转移条件", form.getZytj());
        formData.put("目的", form.getMdMcs());
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Out Amount", gspOld.getKc()+ gspOld.getKcdw());
        formDataEn.put("Transfer Condition", form.getZytj());
        formDataEn.put("Purpose", form.getMdMcs());
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gspOld.getId(), JcgjlxEnum.lc.getValue(), "领取发放", "Distribution", JcmcysEnum.green.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn),
                ffr1);
        //endregion
        //region 台账
        gspTzService.saveFfTz(gspOld.getId(), lqr1, lqr2, ffr1,ffr2, ffr1, "领取发放",
                "Distribution", gspOld.getKc(), gspOld.getKcdw(), form.getRemark());
        //endregion
        //region 更新发放信息
        gspOld.setFfzytj(form.getZytj());
        gspOld.setLqr1Id(lqr1.getUserId());
        gspOld.setLqr1Mc(lqr1.getNickName());
        if(lqr2 != null) {
            gspOld.setLqr2Id(lqr2.getUserId());
            gspOld.setLqr2Mc(lqr2.getNickName());
        }
        gspOld.setFfr1Id(ffr1.getUserId());
        gspOld.setFfr1Mc(ffr1.getNickName());
        if(ffr2 != null) {
            gspOld.setFfr2Id(ffr2.getUserId());
            gspOld.setFfr2Mc(ffr2.getNickName());
        }
        gspOld.setFfrq(now);
        gspOld.setFfbz(form.getRemark());
        gspOld.setMdIds(form.getMdMcs());
        gspOld.setMdOther(form.getMdOther());
        gspOld.setZjzt(ZjztEnum.yff.getValue());
        //endregion
        this.updateById(gspOld);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plff(GspPlffForm form) {
        //验证领取人1、领取人2、发放人1、发放人2密码
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        sysUserService.checkPassword(lqr1, form.getLqr1mm(), true);
        SysUser lqr2 = null;
        if(form.getLqr2Id() != null) {
            lqr2 = sysUserService.selectUserById(form.getLqr2Id());
            sysUserService.checkPassword(lqr2, form.getLqr2mm(), true);
        }
        SysUser ffr1 = sysUserService.selectUserById(form.getFfr1Id());
        sysUserService.checkPassword(ffr1, form.getFfr1mm(), true);
        SysUser ffr2 = null;
        if(form.getFfr2Id() != null) {
            ffr2 = sysUserService.selectUserById(form.getFfr2Id());
            sysUserService.checkPassword(ffr2, form.getFfr2mm(), true);
        }

        List<Long> gspIdList = form.getList().stream().map(GspPlffItem::getId).collect(Collectors.toList());

        QueryWrapper<Gsp> queryWrapper = Wrappers.query();
        queryWrapper.in("id", gspIdList);
        queryWrapper.eq("zjzt", ZjztEnum.rk.getValue());
        List<Gsp> gspList = this.list(queryWrapper);
        List<GspJcgj> gspJcgjList = new ArrayList<>();
        List<GspTz> mjyTzList = new ArrayList<>();
        if (gspList.size() <= form.getList().size()) {
            throw new ServiceException("存在供试品不是入库状态，不能发放");
        }
        Date now = new Date();
        for (Gsp gsp : gspList) {
            List<GspPlffItem> plffItemFilter = form.getList().stream()
                    .filter(p -> p.getId().equals(gsp.getId()))
                    .collect(Collectors.toList());
            if (plffItemFilter.size() > 0) {
                gspFfjlService.ff(gsp.getKc(),gsp.getKcdw(),plffItemFilter.get(0).getZytj(),plffItemFilter.get(0).getRemark(),plffItemFilter.get(0).getMdIds(),gsp,lqr1,lqr2,ffr1,ffr2,now);
                //region 稽查轨迹
                GspJcgj gspJcgj = new GspJcgj();
                gspJcgj.setGspId(gsp.getId());
                gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
                gspJcgj.setJcmc("领取发放");
                gspJcgj.setJcmcEn("Distribution");
                gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("出库量", gsp.getKc()+ gsp.getKcdw());
                formData.put("转移条件", plffItemFilter.get(0).getZytj());
                formData.put("目的", plffItemFilter.get(0).getMdMcs());
                formData.put("备注", plffItemFilter.get(0).getRemark());
                gspJcgj.setJcnr(JctUtil.formatStr(formData));
                Map<String, String> formDataEn = new LinkedHashMap<>();
                formDataEn.put("Out Amount", gsp.getKc()+ gsp.getKcdw());
                formDataEn.put("Transfer Condition", plffItemFilter.get(0).getZytj());
                formDataEn.put("Purpose", plffItemFilter.get(0).getMdMcs());
                formDataEn.put("Comment", plffItemFilter.get(0).getRemark());
                gspJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
                gspJcgj.setQmrId(SecurityUtils.getUserId());
                gspJcgj.setQmrMc(SecurityUtils.getNickName());
                gspJcgj.setQmrMcEn(SecurityUtils.getUsername());
                gspJcgjList.add(gspJcgj);
                //endregion
                //region 台账
                GspTz gspTz = new GspTz();
                gspTz.setGspId(gsp.getId());
                gspTz.setLqrMc(lqr2 == null ? lqr1.getNickName() : lqr1.getNickName() + "," + lqr2.getNickName());
                gspTz.setLqrMcEn(lqr2 == null ? lqr1.getUserName() : lqr1.getUserName() + "," + lqr2.getUserName());
                gspTz.setFfrMc(ffr2 == null ? ffr1.getNickName() : ffr1.getNickName() + "," + ffr2.getNickName());
                gspTz.setFfrMcEn(ffr2 == null ? ffr1.getUserName() : ffr1.getUserName() + "," + ffr2.getUserName());
                gspTz.setQmyy("领取发放");
                gspTz.setQmyyEn("Distribution");
                gspTz.setCzl(gsp.getKc());
                gspTz.setCzldw(gsp.getKcdw());
                gspTz.setQmrId(ffr1.getUserId());
                gspTz.setQmrMc(ffr1.getNickName());
                gspTz.setQmrMcEn(ffr1.getUserName());
                gspTz.setRemark(plffItemFilter.get(0).getRemark());
                mjyTzList.add(gspTz);
                //endregion
                //region 更新发放信息
                gsp.setFfzytj(plffItemFilter.get(0).getZytj());
                gsp.setLqr1Id(lqr1.getUserId());
                gsp.setLqr1Mc(lqr1.getNickName());
                gsp.setLqr2Id(lqr2 == null ? null : lqr2.getUserId());
                gsp.setLqr2Mc(lqr2 == null ? "" : lqr2.getNickName());
                gsp.setFfr1Id(ffr1.getUserId());
                gsp.setFfr1Mc(ffr1.getNickName());
                gsp.setFfr2Id(ffr2 == null ? null : ffr2.getUserId());
                gsp.setFfr2Mc(ffr2 == null ? "" : ffr2.getNickName());
                gsp.setFfrq(now);
                gsp.setFfbz(plffItemFilter.get(0).getRemark());
                gsp.setMdIds(plffItemFilter.get(0).getMdIds());
                gsp.setMdOther(plffItemFilter.get(0).getMdOther());
                gsp.setZjzt(ZjztEnum.yff.getValue());
                //endregion
            } else {
                throw new ServiceException("供试品【" + gsp.getMc() + "】不存在或已删除");
            }
        }
        gspJcgjService.saveBatch(gspJcgjList);
        gspTzService.saveBatch(mjyTzList);
        this.saveOrUpdateBatch(gspList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void xgkc(GspXgkcForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getKcbjzt().equals(KcbjEnum.wbj.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】已经提交修改库存申请，请等待审核");
        }
        if (!(gspOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gspOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是入库状态，不能修改库存");
        }
        List<ObjectCompareUtil.FieldChange> fieldChanges = ObjectCompareUtil.compareObjects(gspOld, form);
        if (fieldChanges.size() == 0) {
            throw new ServiceException("你没有修改任何内容");
        }
        List<GspJcgj> gspJcgjList = new ArrayList<>();
        //region 稽查轨迹
        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            GspJcgj gspJcgj = new GspJcgj();
            gspJcgj.setGspId(gspOld.getId());
            gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspJcgj.setJcmc("修改库存申请");
            gspJcgj.setJcmcEn("Apply for Change Inventory");
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            gspJcgj.setJcnr(fieldChange.toString());
            gspJcgj.setQmrId(qmr.getUserId());
            gspJcgj.setQmrMc(qmr.getNickName());
            gspJcgj.setQmrMcEn(qmr.getUserName());
            gspJcgj.setRemark(form.getKcbjbz());
            gspJcgjList.add(gspJcgj);
        }
        //endregion

        gspOld.setKcdwbj(form.getKcdw());
        gspOld.setKcbj(form.getKc());
        gspOld.setKcbjrMc(qmr.getNickName());
        gspOld.setKcbjbz(form.getKcbjbz());
        gspOld.setKcbjzt(KcbjEnum.shz.getValue());
        gspOld.setKcbjrId(qmr.getUserId());
        gspOld.setKcbjrMc(qmr.getNickName());
        this.updateById(gspOld);
        gspJcgjService.saveBatch(gspJcgjList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void shxgkc(GspShxgkcForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getId() == null || form.getId() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getKcbjzt().equals(KcbjEnum.shz.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】没有提交修改库存申请，不能审核修改库存");
        }
        if (!(gspOld.getZjzt().equals(ZjztEnum.rk.getValue()) || gspOld.getZjzt().equals(ZjztEnum.yff.getValue()))) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是入库或已发放状态，不能审核修改库存");
        }
        //region 稽查轨迹
        GspJcgj gspJcgj = new GspJcgj();
        gspJcgj.setGspId(gspOld.getId());
        gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            gspJcgj.setJcmc( ShXgkcEnum.ty.getText());
            gspJcgj.setJcmcEn("Approve Inventory Change");
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            noticeService.save("供试品【"+gspOld.getMc()+"】同意修改库存",gspOld.getKcbjrId(),getUrlQz("gspList"));

        } else {
            gspJcgj.setJcmc( ShXgkcEnum.jj.getText());
            gspJcgj.setJcmcEn("Reject Inventory Change");
            gspJcgj.setJcmcys(JcmcysEnum.red.getValue());
            noticeService.save("供试品【"+gspOld.getMc()+"】拒绝修改库存",gspOld.getKcbjrId(),getUrlQz("gspList"));
        }
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("申请备注", gspOld.getKcbjbz());
        formData.put("审核备注", form.getRemark());
        gspJcgj.setJcnr(JctUtil.formatStr(formData));

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Application Comment", gspOld.getBjbz());
        formDataEn.put("Approve Comment", form.getRemark());
        gspJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
        gspJcgj.setQmrId(qmr.getUserId());
        gspJcgj.setQmrMc(qmr.getNickName());
        gspJcgj.setQmrMcEn(qmr.getUserName());
        gspJcgjService.save(gspJcgj);
        //endregion

        //region 更新
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            gspOld.setKcdw(gspOld.getKcdwbj());
            gspOld.setKc(gspOld.getKcbj());
        }
        gspOld.setKcbj(null);
        gspOld.setKcdwbj(null);
        gspOld.setKcbjbz(null);
        gspOld.setKcbjzt(KcbjEnum.wbj.getValue());
        this.updateById(gspOld);
        //endregion

        //region 台账
        if (form.getQmyy().equals(ShXgkcEnum.ty.getValue())) {
            gspTzService.saveTz(gspOld.getId(), qmr,
                    "修改库存", "Change Inventory", form.getKc(), form.getKcdw(), gspOld.getKcbjbz());
        }
        //endregion
    }

    @Override
    public void plsd(GspSdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getIds() == null || form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<Gsp> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Gsp> gspList = this.list(queryWrapper);
        List<GspJcgj> gspJcgjList = new ArrayList<>();
        for (Gsp gsp : gspList) {
            if (!gsp.getZjzt().equals(ZjztEnum.rk.getValue())) {
                throw new ServiceException("供试品【" + gsp.getMc() + "】不是入库状态，不能锁定");
            }
            //锁定
            gsp.setZjzt(ZjztEnum.ysd.getValue());

            //region 稽查轨迹
            GspJcgj gspJcgj = new GspJcgj();
            gspJcgj.setGspId(gsp.getId());
            gspJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspJcgj.setJcmc("锁定供试品");
            gspJcgj.setJcmcEn("Lock");
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getRemark());
            gspJcgj.setJcnr(JctUtil.formatStr(formData));

            Map<String, String> formDataEn = new LinkedHashMap<>();
            formDataEn.put("Comment", form.getRemark());
            gspJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
            gspJcgj.setQmrId(qmr.getUserId());
            gspJcgj.setQmrMc(qmr.getNickName());
            gspJcgj.setQmrMcEn(qmr.getUserName());
            gspJcgjList.add(gspJcgj);
            //endregion

        }
        this.saveOrUpdateBatch(gspList);
        gspJcgjService.saveBatch(gspJcgjList);
    }

    @Override
    public void sd(GspSdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是入库状态，不能锁定");
        }
        gspOld.setZjzt(ZjztEnum.ysd.getValue());
        this.updateById(gspOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gspOld.getId(), JcgjlxEnum.lc.getValue(), "锁定供试品", "Lock ",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @Override
    public void js(GspJsForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        gspOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(gspOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gspOld.getId(), JcgjlxEnum.lc.getValue(), "解锁供试品", "Unlock ",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cz(GspCzForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getId() == null || form.getId() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gspOld.getZjzt().equals(ZjztEnum.rk.getValue())) {
            throw new SecurityException("供试品【" + gspOld.getMc() + "】不是入库状态，不能处置");
        }
        if(!NumberUtils.isParsable(form.getCzl())) {
            throw new ServiceException("请输入正确的处置量");
        }
        BigDecimal czl = new BigDecimal(form.getCzl());
        BigDecimal kc = new BigDecimal(gspOld.getKc());
        if(czl.compareTo(kc) > 0) {
            throw new ServiceException("处置量不应大于库存量");
        }
        kc = kc.subtract(czl);
        gspOld.setKc(kc.toString());
        updateById(gspOld);


        GspJcgj gspJcgj = new GspJcgj();
        gspJcgj.setGspId(gspOld.getId());
        gspJcgj.setJcgjlx(JcgjlxEnum.bj.getValue());
        gspJcgj.setJcmc("处置");
        gspJcgj.setJcmcEn("Dispose");
        LinkedHashMap<String, String> jcnrMap = new LinkedHashMap<>();
        jcnrMap.put("处置方式", form.getCzfs());
        jcnrMap.put("处置量", form.getCzl());
        jcnrMap.put("原因", form.getRemark());
        gspJcgj.setJcnr(JctUtil.formatStr(jcnrMap));
        LinkedHashMap<String, String> jcnrMapEn = new LinkedHashMap<>();
        jcnrMapEn.put("Dispose Method", form.getCzfs());
        jcnrMapEn.put("Amount", form.getCzl());
        jcnrMapEn.put("Reason", form.getRemark());
        gspJcgj.setJcnrEn(JctUtil.formatStr(jcnrMapEn));
        gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
        gspJcgj.setQmrId(qmr.getUserId());
        gspJcgj.setQmrMc(qmr.getNickName());
        gspJcgj.setQmrMcEn(qmr.getUserName());
        gspJcgjService.save(gspJcgj);

        // 台账
        gspTzService.saveTz(gspOld.getId(), qmr,
                "处置", "Dispose", form.getCzl(), gspOld.getKcdw(), form.getRemark());

    }

    @Override
    public void plcz(GspPlczForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        List<Gsp> gspList = new ArrayList<>();
        Map<Long, Gsp> map = new HashMap<>();
        for (GspPlczItemForm itemForm : form.getList()) {
            if (itemForm.getId() == null || itemForm.getId() < 0) {
                throw new SecurityException("参数id不正确");
            }
            Gsp gsp = this.getById(itemForm.getId());
            if (gsp == null) {
                throw new SecurityException("供试品不存在或已删除");
            }
            if (!gsp.getZjzt().equals(ZjztEnum.rk.getValue())) {
                throw new SecurityException("供试品【" + gsp.getMc() + "】不是入库状态，不能处置");
            }
            if(!NumberUtils.isParsable(itemForm.getCzl())) {
                throw new ServiceException("请输入正确的处置量");
            }
            BigDecimal czl = new BigDecimal(itemForm.getCzl());
            BigDecimal kc = new BigDecimal(gsp.getKc());
            if(czl.compareTo(kc) > 0) {
                throw new ServiceException("处置量不应大于库存量");
            }
            kc = kc.subtract(czl);
            gsp.setKc(kc.toString());
            gspList.add(gsp);

            map.put(gsp.getId(), gsp);
        }
        updateBatchById(gspList);

        List<GspJcgj> gspJcgjList = new ArrayList<>();
        List<GspTz> gspTzs = new ArrayList<>();
        for (GspPlczItemForm itemForm : form.getList()) {
            Gsp gsp = map.get(itemForm.getId());

            GspJcgj gspJcgj = new GspJcgj();
            gspJcgj.setGspId(gsp.getId());
            gspJcgj.setJcgjlx(JcgjlxEnum.bj.getValue());
            gspJcgj.setJcmc("处置");
            gspJcgj.setJcmcEn("Dispose");
            LinkedHashMap<String, String> jcnrMap = new LinkedHashMap<>();
            jcnrMap.put("处置方式", itemForm.getCzfs());
            jcnrMap.put("处置量", itemForm.getCzl());
            jcnrMap.put("原因", itemForm.getRemark());
            gspJcgj.setJcnr(JctUtil.formatStr(jcnrMap));
            LinkedHashMap<String, String> jcnrMapEn = new LinkedHashMap<>();
            jcnrMapEn.put("Dispose Method", itemForm.getCzfs());
            jcnrMapEn.put("Amount", itemForm.getCzl());
            jcnrMapEn.put("Reason", itemForm.getRemark());
            gspJcgj.setJcnrEn(JctUtil.formatStr(jcnrMapEn));
            gspJcgj.setJcmcys(JcmcysEnum.green.getValue());
            gspJcgj.setQmrId(qmr.getUserId());
            gspJcgj.setQmrMc(qmr.getNickName());
            gspJcgj.setQmrMcEn(qmr.getUserName());
            gspJcgjList.add(gspJcgj);

            // 台账
            GspTz gspTz = new GspTz();
            gspTz.setGspId(gsp.getId());
            gspTz.setQmrId(qmr.getUserId());
            gspTz.setQmrMc(qmr.getNickName());
            gspTz.setQmrMcEn(qmr.getUserName());
            gspTz.setQmyy("处置");
            gspTz.setQmyyEn("Dispose");
            gspTz.setCzl(itemForm.getCzl());
            gspTz.setCzldw(gsp.getKcdw());
            gspTz.setRemark(itemForm.getRemark());
            gspTzs.add(gspTz);
        }
        gspJcgjService.saveBatch(gspJcgjList);
        gspTzService.saveBatch(gspTzs);
    }

    @Override
    public void jy(GspJyForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是归档状态，不能借阅");
        }
        if (!gsp.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是未借阅状态，不能借阅");
        }
        if(!gsp.getDasqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        gsp.setJyzt(JyztEnum.djy.getValue());
        gsp.setJyksrq(form.getStartDate());
        gsp.setJyjsrq(form.getEndDate());
        gsp.setDasqrId(qmr.getUserId());
        gsp.setDasqrMc(qmr.getNickName());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getStartDate()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getEndDate()));
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Start Date", DateUtils.dateTime(form.getStartDate()));
        formDataEn.put("End Date", DateUtils.dateTime(form.getEndDate()));
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", "Apply for Check-out",
                JcmcysEnum.orange.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "申请借阅");

    }

    @Override
    public void jd(GspJdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.gd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】未归档，不能解档");
        }
        if(!gsp.getDasqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        //申请解档后，制剂状态更新为待解档，等待档案员审核期间无法进行其他操作；
        gsp.setZjzt(ZjztEnum.djd.getValue());
        gsp.setDasqrIdOld(gsp.getDasqrId());
        gsp.setDasqrMcOld(gsp.getDasqrMc());
        gsp.setDasqrId(qmr.getUserId());
        gsp.setDasqrMc(qmr.getNickName());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "申请解档", "申请解档",
                JcmcysEnum.orange.getValue(), JctUtil.formatStr(formData),  JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "申请解档");

    }

    @Override
    public void gd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.ysd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        gsp.setZjzt(ZjztEnum.dgd.getValue());
        gsp.setDasqrId(qmr.getUserId());
        gsp.setDasqrMc(qmr.getNickName());
        gsp.setGdsqsj(new Date());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "申请归档",  "Apply for Archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "申请归档");
    }

    @Override
    public void plgd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        List<Gsp> gspList = new ArrayList<>();
        for (Long id : form.getIds()) {
            if (id == null || id < 0) {
                throw new SecurityException("参数id不正确");
            }
            Gsp gsp = this.getById(form.getId());
            if (gsp == null) {
                throw new SecurityException("供试品不存在或已删除");
            }
            if (!gsp.getZjzt().equals(ZjztEnum.ysd.getValue())) {
                throw new SecurityException("供试品【" + gsp.getMc() + "】不是锁定状态，不能归档");
            }
            //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
            gsp.setZjzt(ZjztEnum.dgd.getValue());
            gsp.setDasqrId(qmr.getUserId());
            gsp.setDasqrMc(qmr.getNickName());
            gsp.setGdsqsj(new Date());
            gspList.add(gsp);
        }
        this.updateBatchById(gspList);

        //稽查轨迹
        for (Gsp gsp : gspList) {
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getRemark());

            Map<String, String> formDataEn = new LinkedHashMap<>();
            formDataEn.put("Comment", form.getRemark());
            gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "申请归档",  "Apply for Archiving",
                    JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

            archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "申请归档");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gh(GspGhForm form) {
        SysUser ghr1 = sysUserService.selectUserById(form.getGhr1Id());
        sysUserService.checkPassword(ghr1, form.getGhr1mm(), true);
        SysUser ghr2 = null;
        if(form.getGhr2Id() != null) {
            ghr2 = sysUserService.selectUserById(form.getGhr2Id());
            sysUserService.checkPassword(ghr2, form.getGhr2mm(), true);
        }
        SysUser jsr1 = sysUserService.selectUserById(form.getJsr1Id());
        sysUserService.checkPassword(jsr1, form.getJsr1mm(), true);
        SysUser jsr2 = null;
        if(form.getJsr2Id() != null) {
            jsr2 = sysUserService.selectUserById(form.getJsr2Id());
            sysUserService.checkPassword(jsr2, form.getJsr2mm(), true);
        }
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        Gsp gspOld = this.getById(form.getId());
        if (gspOld == null) {
            throw new ServiceException("供试品不存在或已删除");
        }
        if (!gspOld.getZjzt().equals(ZjztEnum.yff.getValue())) {
            throw new ServiceException("供试品【" + gspOld.getMc() + "】不是已发放状态，不能归还");
        }
        gspOld.setKc(form.getRkl());
        gspOld.setKcdw(form.getRkldw());
        gspOld.setZjzt(ZjztEnum.rk.getValue());
        this.updateById(gspOld);

        gspFfjlService.gh(form,gspOld,ghr1,ghr2,jsr1,jsr2);
        //region 台账
        gspTzService.saveGhTz(gspOld.getId(),ghr1, ghr2, jsr1, jsr2, jsr1,
                "归还", "Return", gspOld.getKc(), gspOld.getKcdw(), form.getRemark());
        //endregion
        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("归还转移条件", form.getGhzytj());
        formData.put("入库量", form.getRkl()+form.getRkldw());
        formData.put("归还人1", ghr1.getNickName());
        if(ghr2 != null) {
            formData.put("归还人2", ghr2.getNickName());
        }
        formData.put("库管员1", jsr1.getNickName());
        if(jsr2 != null) {
            formData.put("库管员2", jsr2.getNickName());
        }
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Transfer Condition", form.getGhzytj());
        formDataEn.put("Amount out", form.getRkl()+form.getRkldw());
        formDataEn.put("Returner", ghr1.getNickName());
        if(ghr2 != null) {
            formDataEn.put("Returner 2", ghr2.getNickName());
        }
        formDataEn.put("Warehouse Keeper", jsr1.getNickName());
        if(jsr2 != null) {
            formDataEn.put("Warehouse Keeper 2", jsr2.getNickName());
        }
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gspOld.getId(), JcgjlxEnum.lc.getValue(), "归还", "Return",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), jsr1);
        //endregion

        noticeService.save("供试品【"+gspOld.getMc()+"】确认归还",ghr1.getUserId(),getUrlQz("gspList"));
        if(ghr2 != null) {
            noticeService.save("供试品【"+gspOld.getMc()+"】确认归还",ghr2.getUserId(),getUrlQz("gspList"));
        }

    }

    @Override
    public List<GspListDto> queryStudyList(GspSearchListForm form) {
        QueryWrapper<Gsp> 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 (com.hxhq.common.core.utils.StringUtils.isNoneBlank(form.getMc())) {
            queryWrapper.and(p -> p.like("t.`mc`", form.getMc()));
        }
        if (com.hxhq.common.core.utils.StringUtils.isNoneBlank(form.getBh())) {
            queryWrapper.and(p -> p.like("t.`bh`", form.getBh()));
        }
        if (com.hxhq.common.core.utils.StringUtils.isNoneBlank(form.getStartDate())) {
            queryWrapper.apply("t.yxq>={0}", form.getStartDate());
        }
        if (com.hxhq.common.core.utils.StringUtils.isNoneBlank(form.getEndDate())) {
            queryWrapper.apply("t.yxq<{0}", form.getEndDate());
        }
        queryWrapper.orderByDesc("ts.id");
        return baseMapper.queryStudyList(queryWrapper);
    }

    @Override
    public void agreeGd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待归档状态，不能操作");
        }
        gsp.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "同意归档",  "Approve Archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "同意归档");

        noticeService.save("供试品【"+gsp.getMc()+"】同意归档",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void refuseGd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.dgd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待归档状态，不能操作");
        }
        gsp.setZjzt(ZjztEnum.ysd.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档",  "Reject Archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "拒绝归档");

        noticeService.save("供试品【"+gsp.getMc()+"】拒绝归档",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void agreeJd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待解档状态，不能操作");
        }
        gsp.setZjzt(ZjztEnum.ysd.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "同意解档",  "Approve De-archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "同意解档");

        noticeService.save("供试品【"+gsp.getMc()+"】同意解档",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void refuseJd(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getZjzt().equals(ZjztEnum.djd.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待解档状态，不能操作");
        }
        gsp.setDasqrMc(gsp.getDasqrMcOld());
        gsp.setDasqrId(gsp.getDasqrIdOld());
        gsp.setZjzt(ZjztEnum.gd.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档",  "Reject De-archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "拒绝解档");

        noticeService.save("供试品【"+gsp.getMc()+"】拒绝解档",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void agreeJy(GspJyForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待借阅状态，不能操作");
        }
        gsp.setJyzt(JyztEnum.jyz.getValue());
        gsp.setJyksrq(form.getStartDate());
        gsp.setJyjsrq(form.getEndDate());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "同意借阅",  "Approve Check-out",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "同意借阅");

        noticeService.save("供试品【"+gsp.getMc()+"】同意借阅",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void refuseJy(GspJyForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是待借阅状态，不能操作");
        }
        gsp.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅",  "Reject Check-out",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "拒绝借阅");

        noticeService.save("供试品【"+gsp.getMc()+"】拒绝借阅",gsp.getDasqrId(),getUrlQz("gspList"));

    }

    @Override
    public void archiveGh(GspGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Gsp gsp = this.getById(form.getId());
        if (gsp == null) {
            throw new SecurityException("供试品不存在或已删除");
        }
        if (!gsp.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new SecurityException("供试品【" + gsp.getMc() + "】不是借阅中状态，不能操作");
        }
        gsp.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gsp);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "确认归还",  "Check-out End Confirmation",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsp, gsp.getId(), gsp.getMc(), gsp.getPh(), gsp.getGg() + gsp.getGgdw(), "确认归还");

        noticeService.save("供试品【"+gsp.getMc()+"】确认归还",gsp.getDasqrId(),getUrlQz("gspList"));

    }

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

}
