package com.hxhq.business.service.impl;

import java.util.*;
import java.util.stream.Collectors;

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.mjy.FfjlDetailDto;
import com.hxhq.business.dto.mjy.FfjlListDto;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.study.StudyTypeEnum;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.enums.zykgl.JlztEnum;
import com.hxhq.business.form.mjy.*;
import com.hxhq.business.service.*;
import com.hxhq.business.service.IStudyMjyService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.MjyFfjlMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发放记录-发放记录Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-21
 */
@Service
public class MjyFfjlServiceImpl extends ServiceImpl<MjyFfjlMapper, MjyFfjl> implements IMjyFfjlService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IMjyFfjlJcgjService mjyFfjlJcgjService;
    @Autowired
    private IStudyMjyService studyMjyService;
    @Autowired
    private IArchiveLogService archiveLogService;
    @Autowired
    private INoticeService noticeService;

    //region 发放记录

    /**
     * 详情
     *
     * @param mjyId
     * @return
     */
    @Override
    public MjyFfjl queryLastInfoByMjyId(Long mjyId) {
        QueryWrapper<MjyFfjl> queryWrapper = Wrappers.query();
        queryWrapper.eq("mjy_id", mjyId);
        queryWrapper.orderByDesc("id");
        return this.getOne(queryWrapper, false);
    }

    /**
     * 列表
     *
     * @param form 发放记录
     * @return 发放记录
     */
    @Override
    public List<FfjlListDto> queryList(FfjlSearchForm form) {
        QueryWrapper<MjyFfjl> 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.rkrq>={0}", form.getStartDateRk()));
        }
        if (StringUtils.isNoneBlank(form.getEndDateRk())) {
            queryWrapper.apply("t.ghrq>={0}", form.getEndDateRk());
        }
        if (StringUtils.isNoneBlank(form.getMdMc())) {
            queryWrapper.and(p -> p.like("t.md_other", form.getMdMc())
                    .or().apply(" exists(select s.* from t_study s  where find_in_set(s.id, t.`md_ids`) and  s.name like CONCAT('%', {0}, '%'))",form.getMdMc())
            );
        }
        queryWrapper.orderByDesc("t.id");
        return baseMapper.queryList(queryWrapper);
    }

    /**
     * 加签 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jq(FfjlJqForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是未锁定状态，不能加签");
        }
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.bcsm.getValue(), "补充说明", JcmcysEnum.orange.getValue(), null, qmr,form.getRemark());
    }

    /**
     * 借阅 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jy(FfjlJyForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是归档状态，不能借阅");
        }
        if (!mjyFfjlOld.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是未借阅状态，不能借阅");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        mjyFfjlOld.setJyzt(JyztEnum.djy.getValue());
        mjyFfjlOld.setJyksrq(form.getStartDate());
        mjyFfjlOld.setJyjsrq(form.getEndDate());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getStartDate()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getEndDate()));
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", JcmcysEnum.orange.getValue(), formData, qmr,form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "申请借阅");
    }

    /**
     * 详情
     *
     * @param id
     * @return 暂存柜
     */
    @Override
    public FfjlDetailDto queryInfo(Long id) {
        if (id == null || id.longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        return baseMapper.queryInfo(id);
    }

    /**
     * 解档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void jd(FfjlJdForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】未归档，不能解档");
        }
        //申请解档后，状态更新为待解档，等待档案员审核期间无法进行其他操作；
        mjyFfjlOld.setJlzt(JlztEnum.djd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请解档", JcmcysEnum.green.getValue(), null, qmr,form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "申请解档");
    }

    /**
     * 批量归档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plgd(FfjlGdForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<MjyFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<MjyFfjl> mjyFfjlList = this.list(queryWrapper);
        List<MjyFfjlJcgj> mjyFfjlJcgjList = new ArrayList<>();
        if (mjyFfjlList.size() > 0) {
            for (MjyFfjl mjy : mjyFfjlList) {
                if (!mjy.getJlzt().equals(JlztEnum.ysd.getValue())) {
                    throw new ServiceException("发放回收记录【" + mjy.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (MjyFfjl mjyFfjl : mjyFfjlList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                mjyFfjl.setJlzt(JlztEnum.dgd.getValue());
                mjyFfjl.setDazt(DaztEnum.ygd.getValue());
                mjyFfjl.setGdsqrId(qmr.getUserId());
                mjyFfjl.setGdsqrMc(qmr.getNickName());
                // 稽查轨迹
                mjyFfjlJcgjList.add(mjyFfjlJcgjService.getJcgj(mjyFfjl.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.green.getValue(), null, qmr,form.getRemark()));
                //保存日志
                archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjl.getId(), mjyFfjl.getMc(), mjyFfjl.getBh(), mjyFfjl.getNd()+mjyFfjl.getNddw(), "申请解档");
            }
            this.updateBatchById(mjyFfjlList);
            mjyFfjlJcgjService.saveBatch(mjyFfjlJcgjList);
        }
    }

    /**
     * 归档 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gd(FfjlGdForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        mjyFfjlOld.setJlzt(JlztEnum.dgd.getValue());
        mjyFfjlOld.setDazt(DaztEnum.ygd.getValue());
        mjyFfjlOld.setGdsqrId(qmr.getUserId());
        mjyFfjlOld.setGdsqrMc(qmr.getNickName());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.green.getValue(), null, qmr,form.getRemark());
    }


    /**
     * 批量锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void plsd(FfjlSdForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getIds().length < 0) {
            throw new ServiceException("参数ids不正确");
        }
        QueryWrapper<MjyFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("jlzt", JlztEnum.wsd.getValue());
        List<MjyFfjl> mjyFfjlList = this.list(queryWrapper);
        List<MjyFfjlJcgj> mjyFfjlJcgjList = new ArrayList<>();
        if (mjyFfjlList.size() <= 0) {
            throw new ServiceException("发放回收记录不是未锁定状态，不能锁定");
        }
        for (MjyFfjl mjyFfjl : mjyFfjlList) {
            //锁定
            mjyFfjl.setJlzt(JlztEnum.ysd.getValue());
            // 稽查轨迹
            mjyFfjlJcgjList.add(mjyFfjlJcgjService.getJcgj(mjyFfjl.getId(), JcgjlxEnum.lc.getValue(), "锁定发放记录", JcmcysEnum.green.getValue(), null, qmr,form.getRemark()));
        }
        this.updateBatchById(mjyFfjlList);
        mjyFfjlJcgjService.saveBatch(mjyFfjlJcgjList);

    }

    /**
     * 锁定 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sd(FfjlSdForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是未锁定状态，不能锁定");
        }
        mjyFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "锁定发放记录", JcmcysEnum.green.getValue(), null, qmr,form.getRemark());

    }


    /**
     * 解锁 ok
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void js(FfjlJsForm form) {
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        mjyFfjlOld.setJlzt(JlztEnum.wsd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "解锁发放记录", JcmcysEnum.green.getValue(), null, qmr,form.getRemark());

    }

    /**
     * 归还
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gh(GhForm form, Mjy mjy, SysUser ghr1, SysUser ghr2, SysUser jsr1, SysUser jsr2) {
        MjyFfjl mjyFfjl = this.queryLastInfoByMjyId(mjy.getId());
        if (mjyFfjl == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        //region 新增发放记录

        mjyFfjl.setFfzytj(form.getGhzytj());
        mjyFfjl.setRkwz(form.getRkwz());
        mjyFfjl.setRktj(form.getRktj());
        mjyFfjl.setGhzytj(form.getGhzytj());
        mjyFfjl.setRkmz(form.getRkmz());
        mjyFfjl.setRkmzdw(mjy.getKcdw());

        mjyFfjl.setJsl(form.getJsl());
        mjyFfjl.setJsldw(mjy.getKcdw());
        mjyFfjl.setSyl(form.getSyl());
        mjyFfjl.setSyldw(mjy.getKcdw());
        mjyFfjl.setGhr1Id(ghr1.getUserId());
        mjyFfjl.setGhr1Mc(ghr1.getNickName());
        mjyFfjl.setGhrq(new Date());
        mjyFfjl.setGhr2Id(ghr2.getUserId());
        mjyFfjl.setGhr2Mc(ghr2.getNickName());
        mjyFfjl.setJsr1Id(jsr1.getUserId());
        mjyFfjl.setJsr1Mc(jsr1.getNickName());
        mjyFfjl.setJsr2Id(jsr2.getUserId());
        mjyFfjl.setJsr2Mc(jsr2.getNickName());
        mjyFfjl.setGhbz(form.getRemark());
        this.updateById(mjyFfjl);
        //endregion

        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getGhzytj());
        formData.put("入库毛重", form.getRkmz() + mjy.getKcdw());
        formData.put("减少量", form.getJsl() + mjy.getKcdw());
        formData.put("使用量", form.getSyl() + mjy.getKcdw());
        formData.put("归还人1", ghr1.getNickName());
        formData.put("归还人2", ghr2.getNickName());
        formData.put("库管员1", jsr1.getNickName());
        formData.put("库管员2", jsr2.getNickName());
        mjyFfjlJcgjService.saveJcgj(mjyFfjl.getId(), JcgjlxEnum.lc.getValue(), "归还", JcmcysEnum.green.getValue(), formData, SecurityUtils.getLoginUser().getSysUser(),form.getRemark());

    }


    /**
     * 发放
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ff(FfForm form,Mjy mjy, SysUser lqr1, SysUser lqr2, SysUser ffr1, SysUser ffr2, Date time) {
        //region 新增发放记录
        MjyFfjl mjyFfjl = new MjyFfjl();
        mjyFfjl.setMjyId(mjy.getId());
        mjyFfjl.setMc(mjy.getMc());
        mjyFfjl.setBh(mjy.getBh());
        mjyFfjl.setNd(mjy.getNd());
        mjyFfjl.setNddw(mjy.getNddw());
        mjyFfjl.setCctj(mjy.getCctj());
        mjyFfjl.setCcwz(mjy.getCcwz());
        mjyFfjl.setSxrq(mjy.getSxrq());
        mjyFfjl.setCctj(mjy.getCctj());
        mjyFfjl.setCkl(mjy.getKc());
        mjyFfjl.setCkldw(mjy.getKcdw());
        mjyFfjl.setCkmz( form.getCkmz());
        mjyFfjl.setCkmzdw(form.getCkmzdw());
        mjyFfjl.setLqr1Id(lqr1.getUserId());
        mjyFfjl.setLqr2Id(lqr2.getUserId());
        mjyFfjl.setFfr1Id(ffr1.getUserId());
        mjyFfjl.setFfr2Id(ffr2.getUserId());
        mjyFfjl.setLqr1Mc(lqr1.getNickName());
        mjyFfjl.setLqr2Mc(lqr2.getNickName());
        mjyFfjl.setFfr1Mc(ffr1.getNickName());
        mjyFfjl.setFfr2Mc(ffr2.getNickName());
        mjyFfjl.setFfzytj(form.getZytj());
        mjyFfjl.setFfrq(time);
        mjyFfjl.setFfbz(form.getRemark());
        mjyFfjl.setMdIds(form.getMdIds());
        mjyFfjl.setMdOther(form.getMdOther());

        // 新增试验物资
        if(StringUtils.isNoneBlank(form.getMdIds())){
            QueryWrapper<StudyMjy> queryWrapper = Wrappers.query();
            queryWrapper.in("study_id", form.getMdIds());
            queryWrapper.in("resource_id", mjy.getId());
            List<StudyMjy> studyMjysOld = studyMjyService.list(queryWrapper);
            List<StudyMjy> studyMjies=new ArrayList<>();

            String [] tmp=form.getMdIds().split(",");
            for(String s :tmp){
                List<StudyMjy> existList = studyMjysOld.stream()
                        .filter(p -> p.getStudyId().equals(Long.parseLong(s)))
                        .collect(Collectors.toList());
                if (existList.size() == 0) {
                    StudyMjy studyMjy = new StudyMjy();
                    studyMjy.setStudyId(Long.parseLong(s));
                    studyMjy.setResourceId(mjy.getId());
                    studyMjies.add(studyMjy);
                }
            }
            if(studyMjies.size()>0){
                studyMjyService.insertBatch(studyMjies);
            }
        }

        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("出库量", mjy.getKc() + mjy.getKcdw());
        formData.put("转移条件", form.getZytj());
        formData.put("出库毛重", form.getCkmz() + mjy.getKcdw());
        formData.put("目的", form.getMdMcs());

        this.save(mjyFfjl);
        mjyFfjlJcgjService.saveJcgj(mjyFfjl.getId(), JcgjlxEnum.lc.getValue(), "领取发放", JcmcysEnum.green.getValue(), formData, SecurityUtils.getLoginUser().getSysUser(),form.getRemark());

    }

    //endregion

    //region 档案管理

    /**
     * 查询发放记录列表
     *
     * @param form 发放记录
     * @return 发放记录集合
     */
    @Override
    public List<FfjlListDto> queryArchiveList(FfjlSearchForm form){
        QueryWrapper<MjyFfjl> queryWrapper = Wrappers.query();
        queryWrapper.eq("t.del_flag", "0");
        queryWrapper.eq("t.`dazt`", DaztEnum.ygd.getValue());
        if (form.getJyzt() != null && form.getJyzt().intValue() > 0) {
            queryWrapper.eq("t.jyzt", form.getJyzt());
        }
        if (form.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.rkrq>={0}", form.getStartDateRk()));
        }
        if (StringUtils.isNoneBlank(form.getEndDateRk())) {
            queryWrapper.apply("t.ghrq>={0}", form.getEndDateRk());
        }
        if (StringUtils.isNoneBlank(form.getMdMc())) {
            queryWrapper.and(p -> p.like("t.md_other", form.getMdMc())
                    .or().apply(" exists(select s.* from t_study s  where find_in_set(s.id, t.`md_ids`) and  s.name like CONCAT('%', {0}, '%'))",form.getMdMc())
            );
        }
        queryWrapper.orderByDesc("t.id");
        return baseMapper.queryList(queryWrapper);
    }

    /**
     * 同意归档
     */
    @Override
    public void tygd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待归档状态，不能操作");
        }
        mjyFfjlOld.setJlzt(JlztEnum.gd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "同意归档");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】同意归档",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 拒绝归档
     */
    @Override
    public void jjgd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待归档状态，不能操作");
        }
        mjyFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        mjyFfjlOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "拒绝归档");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】拒绝归档",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 同意解档
     */
    @Override
    public void tyjd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待解档状态，不能操作");
        }
        mjyFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        mjyFfjlOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "同意解档");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】同意解档",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 拒绝解档
     */
    @Override
    public void jjjd(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待解档状态，不能操作");
        }
        mjyFfjlOld.setJlzt(JlztEnum.gd.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "拒绝解档");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】拒绝解档",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 同意借阅
     */
    @Override
    public void tyjy(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待借阅状态，不能操作");
        }
        mjyFfjlOld.setJyzt(JyztEnum.jyz.getValue());
        mjyFfjlOld.setJyksrq(form.getJyksrq());
        mjyFfjlOld.setJyjsrq(form.getJyjsrq());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意借阅", JcmcysEnum.green.getValue(), formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "同意借阅");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】同意借阅",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 拒绝借阅
     */
    @Override
    public void jjjy(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是待借阅状态，不能操作");
        }
        mjyFfjlOld.setJyzt(JyztEnum.wjy.getValue());
        mjyFfjlOld.setJyksrq(form.getJyksrq());
        mjyFfjlOld.setJyjsrq(form.getJyjsrq());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        formData.put("原因", form.getRemark());
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "拒绝借阅");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】拒绝借阅",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    /**
     * 确认归还
     */
    @Override
    public void qrgh(ArchiveForm form){
        SysUser qmr=SecurityUtils.getLoginUser().getSysUser();
        //验证签名人密码
        checkPassword(qmr,form.getQmrmm(),false);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new ServiceException("参数id不正确");
        }
        MjyFfjl mjyFfjlOld = this.getById(form.getId());
        if (mjyFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!mjyFfjlOld.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new ServiceException("发放回收记录【" + mjyFfjlOld.getMc() + "】不是借阅中状态，不能操作");
        }
        mjyFfjlOld.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(mjyFfjlOld);
        //稽查轨迹
        mjyFfjlJcgjService.saveJcgj(mjyFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.mjylqff, mjyFfjlOld.getId(), mjyFfjlOld.getMc(), mjyFfjlOld.getBh(), mjyFfjlOld.getNd()+mjyFfjlOld.getNddw(), "确认归还");
        //通知
        noticeService.save("发放回收记录【"+mjyFfjlOld.getMc()+"】确认归还",mjyFfjlOld.getGdsqrId(),getUrlQz("ffjlList"));
    }

    //endregion


    /**
     * 验证密码
     * @param user
     * @param qmrmm
     */
    public void checkPassword(SysUser user,String qmrmm,Boolean needName){
        if(StringUtils.isBlank(qmrmm)){
            throw new ServiceException("签名密码不能为空");
        }
        sysUserService.checkPassword(user,qmrmm,needName);
    }

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