package com.hxhq.business.service.impl;

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

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.GspFfjlListDto;
import com.hxhq.business.dto.mjy.FfjlDetailDto;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.form.gsp.GspGdForm;
import com.hxhq.business.form.gsp.GspGhForm;
import com.hxhq.business.form.gsp.GspJyForm;
import com.hxhq.business.form.gsp.GspSearchListForm;
import com.hxhq.business.form.mjy.*;
import com.hxhq.business.service.IArchiveLogService;
import com.hxhq.business.service.IGspFfjlJcgjService;
import com.hxhq.business.service.INoticeService;
import com.hxhq.business.utils.JctUtil;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.GspFfjlMapper;
import com.hxhq.business.service.IGspFfjlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供试品发放记录Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-30
 */
@Service
public class GspFfjlServiceImpl extends ServiceImpl<GspFfjlMapper, GspFfjl> implements IGspFfjlService
{

    @Autowired
    private IGspFfjlJcgjService gspFfjlJcgjService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IArchiveLogService archiveLogService;

    @Autowired
    private INoticeService noticeService;

    /**
     * 查询供试品发放记录列表
     * @return 供试品发放记录
     */
    @Override
    public List<GspFfjlListDto> queryList(GspSearchListForm form)
    {
        QueryWrapper<GspFfjl> 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.getJlzt() != null && form.getJlzt().intValue() > 0) {
            queryWrapper.eq("t.jlzt", form.getJlzt());
        }
        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.getStartDateCk())) {
            queryWrapper.and(p -> p.apply("t.ffrq>={0}", form.getStartDateCk()));
        }
        if (StringUtils.isNoneBlank(form.getEndDateCk())) {
            queryWrapper.apply("t.ffrq>={0}", form.getStartDateCk());
        }
        if (StringUtils.isNoneBlank(form.getStartDateRk())) {
            queryWrapper.and(p -> p.apply("t.ghrq>={0}", form.getStartDateRk()));
        }
        if (StringUtils.isNoneBlank(form.getEndDateRk())) {
            queryWrapper.apply("t.ghrq>={0}", form.getEndDateRk());
        }
        queryWrapper.orderByDesc("t.id");
        return baseMapper.queryList(queryWrapper);
    }

    @Override
    public GspFfjl queryLastInfoByGspId(Long gspId) {
        QueryWrapper<GspFfjl> queryWrapper = Wrappers.query();
        queryWrapper.eq("gsp_id", gspId);
        queryWrapper.orderByDesc("id");
        return this.getOne(queryWrapper, false);
    }

    @Override
    public void ff(String ckl, String ckdw, String zytj, String remark, String mdIds, Gsp gsp, SysUser lqr1, SysUser lqr2, SysUser ffr1, SysUser ffr2, Date time) {
        GspFfjl gspFfjl = new GspFfjl();
        gspFfjl.setGspId(gsp.getId());
        gspFfjl.setMc(gsp.getMc());
        gspFfjl.setBh(gsp.getBh());
        gspFfjl.setPh(gsp.getPh());
        gspFfjl.setGg(gsp.getGg());
        gspFfjl.setGgdw(gsp.getGgdw());
        gspFfjl.setYxq(gsp.getYxq());
        gspFfjl.setCctj(gsp.getCctj());
        gspFfjl.setJsrq(gsp.getJsrq());
        gspFfjl.setFfzytj(zytj);
        gspFfjl.setCkl(ckl);
        gspFfjl.setCkldw(ckdw);
        gspFfjl.setJyzt(JyztEnum.wjy.getValue());
        gspFfjl.setJlzt(JlztEnum.wsd.getValue());
        gspFfjl.setMdIds(gsp.getMdIds());
        gspFfjl.setMdOther(gsp.getMdOther());
        gspFfjl.setLqr1Id(lqr1.getUserId());
        gspFfjl.setLqr1Mc(lqr1.getNickName());
        if(lqr2 != null) {
            gspFfjl.setLqr2Id(lqr2.getUserId());
            gspFfjl.setLqr2Mc(lqr2.getNickName());
        }
        gspFfjl.setFfr1Id(ffr1.getUserId());
        gspFfjl.setFfr1Mc(ffr1.getNickName());
        if(ffr2 != null) {
            gspFfjl.setFfr2Id(ffr2.getUserId());
            gspFfjl.setFfr2Mc(ffr2.getNickName());
        }
        gspFfjl.setFfzytj(zytj);
        gspFfjl.setFfrq(time);
        gspFfjl.setFfbz(remark);
        gspFfjl.setMdIds(mdIds);
        this.save(gspFfjl);

        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("出库量", gsp.getKc() + gsp.getKcdw());
        formData.put("转移条件", zytj);
        formData.put("目的", mdIds);
        formData.put("备注", remark);

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Out Amount", gsp.getKc() + gsp.getKcdw());
        formDataEn.put("Transfer Condition", zytj);
        formDataEn.put("Purpose", mdIds);
        formDataEn.put("Comment", remark);
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "领取发放", "Distribution",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), ffr1);
        //endregion
    }

    @Override
    public void gh(GspGhForm form, Gsp gsp, SysUser ghr1, SysUser ghr2, SysUser jsr1, SysUser jsr2) {
        GspFfjl gspFfjl = this.queryLastInfoByGspId(gsp.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录不存在或已删除");
        }
        if(!NumberUtils.isParsable(form.getRkl())) {
            throw new ServiceException("请填写正确的入库量");
        }
        BigDecimal rkl = new BigDecimal(form.getRkl());
        BigDecimal ckl = new BigDecimal(gspFfjl.getCkl());
        if(rkl.compareTo(ckl) > 0) {
            throw new ServiceException("入库量不应大于出库量");
        }
        BigDecimal syl = ckl.subtract(rkl);
        gspFfjl.setSyl(syl.toString());
        gspFfjl.setSyldw(gspFfjl.getCkldw());
        gspFfjl.setGhzytj(form.getGhzytj());
        gspFfjl.setRkl(form.getRkl());
        gspFfjl.setRkldw(form.getRkldw());
        gspFfjl.setGhr1Id(ghr1.getUserId());
        gspFfjl.setGhr1Mc(ghr1.getNickName());
        if(ghr2 != null) {
            gspFfjl.setGhr2Id(ghr2.getUserId());
            gspFfjl.setGhr2Mc(ghr2.getNickName());
        }
        gspFfjl.setGhrq(new Date());

        gspFfjl.setJsr1Id(jsr1.getUserId());
        gspFfjl.setJsr1Mc(jsr1.getNickName());
        if(jsr2 != null) {
            gspFfjl.setJsr2Id(jsr2.getUserId());
            gspFfjl.setJsr2Mc(jsr2.getNickName());
        }
        gspFfjl.setGhbz(form.getRemark());
        this.updateById(gspFfjl);
        //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 In", form.getRkl() + form.getRkldw());
        formDataEn.put("Returner1", 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());
        gspFfjlJcgjService.saveJcgj(gsp.getId(), JcgjlxEnum.lc.getValue(), "归还", "Return",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), jsr1);
        //endregion
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jd(FfjlJdForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放记录发放记录【" + gspFfjl.getMc() + "】未归档，不能解档");
        }
        if(!gspFfjl.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        //申请解档后，状态更新为待解档，等待档案员审核期间无法进行其他操作；
        gspFfjl.setJlzt(JlztEnum.djd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "申请解档",  "Apply for De-archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "申请解档");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plgd(FfjlGdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<GspFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<GspFfjl> gspFfjlList = this.list(queryWrapper);
        List<GspFfjlJcgj> gspFfjlJcgjList = new ArrayList<>();
        if (gspFfjlList.size() > 0) {
            for (GspFfjl gspFfjl : gspFfjlList) {
                if (!gspFfjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
                    throw new ServiceException("发放记录发放记录【" + gspFfjl.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (GspFfjl gspFfjl : gspFfjlList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                gspFfjl.setJlzt(JlztEnum.dgd.getValue());
                gspFfjl.setGdsqrId(qmr.getUserId());
                gspFfjl.setGdsqrMc(qmr.getNickName());
                gspFfjl.setDasqrMc(qmr.getNickName());
                gspFfjl.setDasqrId(qmr.getUserId());
                gspFfjl.setGdsqsj(new Date());

                //region 稽查轨迹
                GspFfjlJcgj gspFfjlJcgj = new GspFfjlJcgj();
                gspFfjlJcgj.setFfjlId(gspFfjl.getId());
                gspFfjlJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
                gspFfjlJcgj.setJcmc("申请归档");
                gspFfjlJcgj.setJcmcEn("Apply for Archiving");
                gspFfjlJcgj.setJcmcys(JcmcysEnum.green.getValue());
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("备注", form.getRemark());
                gspFfjlJcgj.setJcnr(JctUtil.formatStr(formData));
                Map<String, String> formDataEn = new LinkedHashMap<>();
                formDataEn.put("Comment", form.getRemark());
                gspFfjlJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
                gspFfjlJcgj.setQmrId(SecurityUtils.getUserId());
                gspFfjlJcgj.setQmrMc(SecurityUtils.getNickName());
                gspFfjlJcgj.setQmrMcEn(SecurityUtils.getUsername());
                gspFfjlJcgjList.add(gspFfjlJcgj);
                //endregion

                GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
                String mdMcs = gspFfjlListDto.getMdMcs();
                if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
                    mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
                }
                archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "申请归档");
            }
            this.saveOrUpdateBatch(gspFfjlList);
            gspFfjlJcgjService.saveBatch(gspFfjlJcgjList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gd(FfjlGdForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放记录发放记录【" + gspFfjl.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        gspFfjl.setJlzt(JlztEnum.dgd.getValue());
        gspFfjl.setGdsqrId(qmr.getUserId());
        gspFfjl.setGdsqrMc(qmr.getNickName());
        gspFfjl.setDasqrMc(qmr.getNickName());
        gspFfjl.setDasqrId(qmr.getUserId());
        gspFfjl.setGdsqsj(new Date());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "申请归档");

    }

    /**
     * 批量锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plsd(FfjlSdForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<GspFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("jlzt", JlztEnum.wsd.getValue());
        List<GspFfjl> gspFfjlList = this.list(queryWrapper);
        List<GspFfjlJcgj> gspFfjlJcgjList = new ArrayList<>();
        if (gspFfjlList.size() <= form.getIds().length) {
            throw new ServiceException("发放记录发放记录不是未锁定状态，不能锁定");
        }
        for (GspFfjl gspFfjl : gspFfjlList) {
            //锁定
            gspFfjl.setJlzt(JlztEnum.ysd.getValue());

            //region 稽查轨迹
            GspFfjlJcgj gspFfjlJcgj = new GspFfjlJcgj();
            gspFfjlJcgj.setFfjlId(gspFfjl.getId());
            gspFfjlJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspFfjlJcgj.setJcmc("锁定发放记录");
            gspFfjlJcgj.setJcmcEn("Lock");
            gspFfjlJcgj.setJcmcys(JcmcysEnum.green.getValue());
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getRemark());
            gspFfjlJcgj.setJcnr(JctUtil.formatStr(formData));
            Map<String, String> formDataEn = new LinkedHashMap<>();
            formDataEn.put("Comment", form.getRemark());
            gspFfjlJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
            gspFfjlJcgj.setQmrId(SecurityUtils.getUserId());
            gspFfjlJcgj.setQmrMc(SecurityUtils.getNickName());
            gspFfjlJcgj.setQmrMcEn(SecurityUtils.getUsername());
            gspFfjlJcgjList.add(gspFfjlJcgj);
            //endregion

        }
        this.saveOrUpdateBatch(gspFfjlList);
        gspFfjlJcgjService.saveBatch(gspFfjlJcgjList);

    }

    /**
     * 锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sd(FfjlSdForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放记录发放记录【" + gspFfjl.getMc() + "】不是未锁定状态，不能锁定");
        }
        gspFfjl.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "锁定发放记录", "Lock",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

    }


    /**
     * 解锁 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void js(FfjlJsForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放记录发放记录【" + gspFfjl.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        gspFfjl.setJlzt(JlztEnum.wsd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "解锁发放记录", "Unlock",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

    }

    /**
     * 借阅 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jy(FfjlJyForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放记录【" + gspFfjl.getMc() + "】不是归档状态，不能借阅");
        }
        if (!gspFfjl.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("发放记录【" + gspFfjl.getMc() + "】不是未借阅状态，不能借阅");
        }
        if(!gspFfjl.getGdsqrId().equals(qmr.getUserId())){
            throw new ServiceException("只有归档申请人才能操作");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        gspFfjl.setJyzt(JyztEnum.djy.getValue());
        gspFfjl.setJyksrq(form.getStartDate());
        gspFfjl.setJyjsrq(form.getEndDate());
        gspFfjl.setDasqrMc(qmr.getNickName());
        gspFfjl.setDasqrId(qmr.getUserId());
        this.updateById(gspFfjl);
        //稽查轨迹
        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());
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", "Apply for Check-out",
                JcmcysEnum.orange.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "申请借阅");

    }

    /**
     * 加签 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jq(FfjlJqForm 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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new ServiceException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放记录【" + gspFfjl.getMc() + "】不是未锁定状态，不能加签");
        }
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspFfjlJcgjService.saveJcgj(gspFfjl.getId(), JcgjlxEnum.lc.getValue(), "补充说明", "Sign",
                JcmcysEnum.orange.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待归档状态，不能操作");
        }
        gspFfjl.setJlzt(JlztEnum.gd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "同意归档");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】同意归档",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待归档状态，不能操作");
        }
        gspFfjl.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "拒绝归档");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】拒绝归档",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待解档状态，不能操作");
        }
        gspFfjl.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "同意解档");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】同意解档",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待解档状态，不能操作");
        }
        gspFfjl.setJlzt(JlztEnum.gd.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "拒绝解档");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】拒绝解档",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待借阅状态，不能操作");
        }
        gspFfjl.setJyzt(JyztEnum.jyz.getValue());
        gspFfjl.setJyksrq(form.getStartDate());
        gspFfjl.setJyjsrq(form.getEndDate());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "同意借阅");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】同意借阅",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是待借阅状态，不能操作");
        }
        gspFfjl.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "拒绝借阅");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】拒绝借阅",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

    @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不正确");
        }
        GspFfjl gspFfjl = this.getById(form.getId());
        if (gspFfjl == null) {
            throw new SecurityException("发放记录不存在或已删除");
        }
        if (!gspFfjl.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new SecurityException("发放记录【" + gspFfjl.getMc() + "】不是借阅中状态，不能操作");
        }
        gspFfjl.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gspFfjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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


        GspFfjlListDto gspFfjlListDto = baseMapper.queryInfo(gspFfjl.getId());
        String mdMcs = gspFfjlListDto.getMdMcs();
        if(StringUtils.isNotEmpty(gspFfjlListDto.getMdOther())) {
            mdMcs = mdMcs + "," + gspFfjlListDto.getMdOther();
        }
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gsplqff, gspFfjl.getId(), gspFfjl.getMc(), gspFfjl.getBh(), mdMcs, "确认归还");
        //通知
        noticeService.save("供试品发放记录【"+gspFfjl.getMc()+"】确认归还",gspFfjl.getGdsqrId(),getUrlQz("ffjlList"));
    }

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

}
