package com.hxhq.business.service.impl;

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

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.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.business.utils.UnitTools;
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;

    public QueryWrapper<GspRkjl> getListCondition(GspSearchListForm form) {
        QueryWrapper<GspRkjl> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(form.getMc())) {
            queryWrapper.like("mc", form.getMc());
        }
        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 queryWrapper;
    }

    /**
     * 查询供试品入库记录列表
     *
     * @return 供试品入库记录
     */
    @Override
    public List<GspRkjl> queryList(GspSearchListForm form)
    {
        QueryWrapper<GspRkjl> queryWrapper = getListCondition(form);
        queryWrapper.orderByDesc("id");
        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());
        BigDecimal basicRkl = UnitTools.convert2Basic(gspRkjl.getRkl(), gspRkjl.getRkdw());
        gspRkjl.setBasicRkl(basicRkl);

        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, JcgjlxEnum.lc.getValue(), "新增供试品入库记录", "Add", JcmcysEnum.blue.getValue(),
                JctUtil.formatStr(formData), JctUtil.formatStr(formDataEn), qmr);
    }

    @Override
    public void addBatch(List<GspRkjlForm> gspRkjlFormList, GspRkjlForm form) {
        SysUser qmr = sysUserService.selectUserById(form.getQmrId());
        sysUserService.checkPassword(qmr, form.getQmrmm(), false);

        List<GspRkjl> gspRkjlList = new ArrayList<>();
        for (GspRkjlForm gspRkjlForm : gspRkjlFormList) {
            GspRkjl gspRkjl = new GspRkjl();
            BeanUtils.copyProperties(gspRkjlForm, gspRkjl);
            gspRkjl.setJlzt(JlztEnum.wsd.getValue());
            gspRkjl.setJyzt(JyztEnum.wjy.getValue());
            BigDecimal basicRkl = UnitTools.convert2Basic(gspRkjl.getRkl(), gspRkjl.getRkdw());
            gspRkjl.setBasicRkl(basicRkl);
            gspRkjlList.add(gspRkjl);
        }
        this.saveBatch(gspRkjlList);


        for (GspRkjl gspRkjl : gspRkjlList) {
            Map<String, String> formData = new LinkedHashMap<>();
            formData.put("备注", form.getQmbz());

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

    @Override
    public GspRkjl queryInfo(GspRkjlForm form) {
        LambdaQueryWrapper<GspRkjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GspRkjl::getMc, form.getMc())
                .eq(GspRkjl::getPh, form.getPh())
                .eq(GspRkjl::getGg, form.getGg())
//                .eq(GspRkjl::getGgdw, form.getGgdw())
                .eq(GspRkjl::getRksj, form.getRksj());
        return getOne(queryWrapper);
    }

    @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.saveBatchWithLog(gspRkjl, 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, 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, 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);
            gspRkjlJcgjService.saveWithLog(gspRkjl, gspRkjlJcgj);

            //endregion

        }
        this.saveOrUpdateBatch(gspRkjlList);
    }

    @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, 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());
                gspRkjl.setGdsqsj(new Date());

                //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());
                gspRkjlJcgjService.saveWithLog(gspRkjl, gspRkjlJcgj);
                gspRkjlJcgjList.add(gspRkjlJcgj);
                //endregion

            }
            this.saveOrUpdateBatch(gspRkjlList);
        }
    }

    @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());
        gspRkjl.setGdsqsj(new Date());
        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, 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, 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, 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, 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, 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, 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, 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, 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, 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"));
    }

    @Override
    public void autoJyExpireReturn() {
        QueryWrapper<GspRkjl> queryWrapper = Wrappers.query();
        queryWrapper.eq("jyzt",JyztEnum.jyz.getValue());
        queryWrapper.apply("jyjsrq<{0}", DateUtils.getTime());
        List<GspRkjl> gspList=this.list(queryWrapper);
        List<GspRkjlJcgj> jcgjList=new ArrayList<>();
        for(GspRkjl gspRkjl:gspList){
            gspRkjl.setJyzt(JyztEnum.wjy.getValue());
            //稽查轨迹
            GspRkjlJcgj jcgj = gspRkjlJcgjService.genJcgj(gspRkjl.getId(), JcgjlxEnum.lc.getValue(), "到期自动归还", "Check-out End on Time",
                    JcmcysEnum.green.getValue(), null, null, null);
            jcgjList.add(jcgj);
        }
        this.updateBatchById(gspList);
        if(jcgjList.size()>0){
            gspRkjlJcgjService.saveBatch(jcgjList);
        }
    }

    @Override
    public String getRklUnit(GspSearchListForm form) {
        QueryWrapper<GspRkjl> queryWrapper = getListCondition(form);
        queryWrapper.isNotNull("rkl")
                .isNotNull("rkdw")
                .ne("rkl", "")
                .ne("rkdw", "");
        Map<String, String> map = baseMapper.getRklUnit(queryWrapper);
        if(map == null) {
            return null;
        }
        return map.get("rkdw");
    }

    @Override
    public String getRkl(GspSearchListForm form) {
        String rklUnit = getRklUnit(form);
        if(StringUtils.isEmpty(rklUnit)) {
            return "";
        }
        QueryWrapper<GspRkjl> queryWrapper = getListCondition(form);
        if (Arrays.asList(UnitTools.VOLUME_UNIT_LIST).contains(rklUnit)) {
            queryWrapper.in("rkdw", UnitTools.VOLUME_UNIT_LIST);
        } else if (Arrays.asList(UnitTools.WEIGHT_UNIT_LIST).contains(rklUnit)) {
            queryWrapper.in("rkdw", UnitTools.WEIGHT_UNIT_LIST);
        } else {
            queryWrapper.eq("rkdw", rklUnit);
        }
        BigDecimal rkl = baseMapper.sumRkl(queryWrapper);
        if(rkl == null) {
            return "";
        }
        rkl = UnitTools.convert(rkl, rklUnit);
        DecimalFormat decimalFormat = new DecimalFormat("#,###.##"); // 定义格式
        String formatted = decimalFormat.format(rkl);
        return formatted + rklUnit;
    }

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