package com.hxhq.business.service.impl;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.*;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.form.gsp.*;
import com.hxhq.business.service.IGspRkjlJcgjService;
import com.hxhq.business.service.INoticeService;
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.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.GspRkjlMapper;
import com.hxhq.business.service.IGspRkjlService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 供试品入库记录Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-30
 */
@Service
public class GspRkjlServiceImpl extends ServiceImpl<GspRkjlMapper, GspRkjl> implements IGspRkjlService
{
    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IGspRkjlJcgjService gspRkjlJcgjService;

    @Autowired
    private INoticeService noticeService;

    /**
     * 查询供试品入库记录列表
     *
     * @return 供试品入库记录
     */
    @Override
    public List<GspRkjl> queryList(GspSearchListForm form)
    {
        QueryWrapper<GspRkjl> 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("rksj", form.getStartDate());
        }
        if(StringUtils.isNotEmpty(form.getEndDate())) {
            queryWrapper.le("rksj", form.getEndDate());
        }
        if(form.getJlzt() != null) {
            queryWrapper.eq("jlzt", form.getJlzt());
        }
        if(form.getJyzt() != null) {
            queryWrapper.eq("jyzt", form.getJyzt());
        }
        if(form.getArchive() != null && form.getArchive() == DaztEnum.ygd.getValue()) {
            queryWrapper.in("jlzt", JlztEnum.dgd.getValue(), JlztEnum.gd.getValue(), JlztEnum.djd.getValue());
        }
        return this.list(queryWrapper);
    }

    @Override
    public void add(GspRkjlForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);
        GspRkjl gspRkjl = new GspRkjl();
        BeanUtils.copyProperties(form, gspRkjl);
        gspRkjl.setJlzt(JlztEnum.wsd.getValue());
        gspRkjl.setJyzt(JyztEnum.wjy.getValue());
        this.save(gspRkjl);

        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getQmbz());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getQmbz());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "新增供试品入库记录", "Add", JcmcysEnum.blue.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

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

        GspRkjl gspRkjl = this.getById(form.getId());
        if(gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是未锁定状态，不能编辑");
        }

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

        BeanUtils.copyProperties(form, gspRkjl);
        this.updateById(gspRkjl);

        List<GspRkjlJcgj> gspRkjlJcgjList = new ArrayList<>();

        for (ObjectCompareUtil.FieldChange fieldChange : fieldChanges) {
            GspRkjlJcgj gspRkjlJcgj = new GspRkjlJcgj();
            gspRkjlJcgj.setGspRkjlId(gspRkjl.getId());
            gspRkjlJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspRkjlJcgj.setJcmc("编辑供试品入库记录");
            gspRkjlJcgj.setJcmcEn("Edit");
            gspRkjlJcgj.setJcmcys(JcmcysEnum.green.getValue());
            gspRkjlJcgj.setJcnr(fieldChange.toString());
            gspRkjlJcgj.setJcnrEn(fieldChange.toEnString());
            gspRkjlJcgj.setQmrId(qmr.getUserId());
            gspRkjlJcgj.setQmrMc(qmr.getNickName());
            gspRkjlJcgj.setRemark(form.getQmbz());
            gspRkjlJcgjList.add(gspRkjlJcgj);
        }
        gspRkjlJcgjService.saveBatch(gspRkjlJcgjList);
    }

    @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不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是未锁定状态，不能锁定");
        }
        gspRkjl.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(gspRkjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.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不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是已锁定状态，不能解锁");
        }
        gspRkjl.setJlzt(JlztEnum.wsd.getValue());
        this.updateById(gspRkjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "解锁供试品入库记录", "Unlock",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);

    }

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

        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<GspRkjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("jlzt", JlztEnum.wsd.getValue());
        List<GspRkjl> gspRkjlList = this.list(queryWrapper);
        List<GspRkjlJcgj> gspRkjlJcgjList = new ArrayList<>();
        if (gspRkjlList.size() <= form.getIds().length) {
            throw new ServiceException("供试品入库记录不是未锁定状态，不能锁定");
        }
        for (GspRkjl gspRkjl : gspRkjlList) {
            //锁定
            gspRkjl.setJlzt(JlztEnum.ysd.getValue());

            //region 稽查轨迹
            GspRkjlJcgj gspRkjlJcgj = new GspRkjlJcgj();
            gspRkjlJcgj.setGspRkjlId(gspRkjl.getId());
            gspRkjlJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
            gspRkjlJcgj.setJcmc("锁定供试品入库记录");
            gspRkjlJcgj.setJcmcEn("Lock");
            gspRkjlJcgj.setJcmcys(JcmcysEnum.green.getValue());
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getRemark());
            gspRkjlJcgj.setJcnr(JctUtil.formatStr(formData));

            Map<String, String> formDataEn = new LinkedHashMap<>();
            formDataEn.put("Comment", form.getRemark());
            gspRkjlJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));

            gspRkjlJcgj.setQmrId(SecurityUtils.getUserId());
            gspRkjlJcgj.setQmrMc(SecurityUtils.getNickName());
            gspRkjlJcgj.setQmrMcEn(SecurityUtils.getUsername());
            gspRkjlJcgjList.add(gspRkjlJcgj);
            //endregion

        }
        this.saveOrUpdateBatch(gspRkjlList);
        gspRkjlJcgjService.saveBatch(gspRkjlJcgjList);
    }

    @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 ServiceException("参数id不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】未归档，不能解档");
        }
        //申请解档后，状态更新为待解档，等待档案员审核期间无法进行其他操作；
        gspRkjl.setJlzt(JlztEnum.djd.getValue());
        this.updateById(gspRkjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

    }

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

        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<GspRkjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<GspRkjl> gspRkjlList = this.list(queryWrapper);
        List<GspRkjlJcgj> gspRkjlJcgjList = new ArrayList<>();
        if (gspRkjlList.size() > 0) {
            for (GspRkjl gspRkjl : gspRkjlList) {
                if (!gspRkjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
                    throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (GspRkjl gspRkjl : gspRkjlList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                gspRkjl.setJlzt(JlztEnum.dgd.getValue());
                gspRkjl.setGdsqrId(qmr.getUserId());
                gspRkjl.setGdsqrMc(qmr.getNickName());

                //region 稽查轨迹
                GspRkjlJcgj gspRkjlJcgj = new GspRkjlJcgj();
                gspRkjlJcgj.setGspRkjlId(gspRkjl.getId());
                gspRkjlJcgj.setJcgjlx(JcgjlxEnum.lc.getValue());
                gspRkjlJcgj.setJcmc("申请归档");
                gspRkjlJcgj.setJcmcEn("Apply for Archiving");
                gspRkjlJcgj.setJcmcys(JcmcysEnum.green.getValue());
                Map<String, String> formData = new LinkedHashMap<>();
                formData.put("备注", form.getRemark());
                gspRkjlJcgj.setJcnr(JctUtil.formatStr(formData));

                Map<String, String> formDataEn = new LinkedHashMap<>();
                formDataEn.put("Comment", form.getRemark());
                gspRkjlJcgj.setJcnrEn(JctUtil.formatStr(formDataEn));
                gspRkjlJcgj.setQmrId(SecurityUtils.getUserId());
                gspRkjlJcgj.setQmrMc(SecurityUtils.getNickName());
                gspRkjlJcgj.setQmrMcEn(SecurityUtils.getUsername());
                gspRkjlJcgjList.add(gspRkjlJcgj);
                //endregion

            }
            this.saveOrUpdateBatch(gspRkjlList);
            gspRkjlJcgjService.saveBatch(gspRkjlJcgjList);
        }
    }

    @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 ServiceException("参数id不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        gspRkjl.setJlzt(JlztEnum.dgd.getValue());
        gspRkjl.setGdsqrId(qmr.getUserId());
        gspRkjl.setGdsqrMc(qmr.getNickName());
        this.updateById(gspRkjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

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

    }

    @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 ServiceException("参数id不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new ServiceException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是归档状态，不能借阅");
        }
        if (!gspRkjl.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("供试品入库记录【" + gspRkjl.getMc() + "】不是未借阅状态，不能借阅");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        gspRkjl.setJyzt(JyztEnum.djy.getValue());
        gspRkjl.setJyksrq(form.getStartDate());
        gspRkjl.setJyjsrq(form.getEndDate());
        this.updateById(gspRkjl);
        //稽查轨迹
        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());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", "Apply for Check-out",
                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不正确");
        }
        GspRkjl gspRkjl = this.getById(form.getId());
        if (gspRkjl == null) {
            throw new SecurityException("供试品入库记录不存在或已删除");
        }
        if (!gspRkjl.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new SecurityException("供试品入库记录【" + gspRkjl.getMc() + "】不是待归档状态，不能操作");
        }
        gspRkjl.setJlzt(ZjztEnum.gd.getValue());
        this.updateById(gspRkjl);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("备注", form.getRemark());

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "同意归档",  "Approve Archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】同意归档",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档",  "Reject Archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】拒绝归档",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "同意解档",  "Approve De-archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】同意解档",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档",  "Reject De-archiving",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】拒绝解档",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "同意借阅",  "Approve Check-out",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】同意借阅",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅",  "Reject Check-out",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】拒绝借阅",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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

        Map<String, String> formDataEn = new LinkedHashMap<>();
        formDataEn.put("Comment", form.getRemark());
        gspRkjlJcgjService.saveJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "确认归还",  "Check-out End Confirmation",
                JcmcysEnum.green.getValue(), JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
        //通知
        noticeService.save("供试品入库记录【"+gspRkjl.getMc()+"】确认归还",gspRkjl.getGdsqrId(),getUrlQz("rkjlList"));
    }

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