package com.hxhq.business.service.impl;

import java.math.BigDecimal;
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.Gyzj;
import com.hxhq.business.domain.GyzjFfjlJcgj;
import com.hxhq.business.domain.StudyGyzj;
import com.hxhq.business.dto.gyzj.FfjlDetailDto;
import com.hxhq.business.dto.gyzj.FfjlListDto;
import com.hxhq.business.dto.gyzj.ListDto;
import com.hxhq.business.enums.archive.ArchiveLogTypeEnum;
import com.hxhq.business.enums.zykgl.*;
import com.hxhq.business.enums.zykgl.JlztEnum;
import com.hxhq.business.form.gyzj.*;
import com.hxhq.business.service.*;
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.GyzjFfjlMapper;
import com.hxhq.business.domain.GyzjFfjl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 发放记录-发放记录Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-21
 */
@Service
public class GyzjFfjlServiceImpl extends ServiceImpl<GyzjFfjlMapper, GyzjFfjl> implements IGyzjFfjlService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IGyzjFfjlJcgjService gyzjFfjlJcgjService;
    @Autowired
    private IStudyGyzjService studyGyzjService;
    @Autowired
    private IArchiveLogService archiveLogService;
    @Autowired
    private INoticeService noticeService;

    //region 发放记录

    /**
     * 详情
     *
     * @param gyzjId
     * @return
     */
    @Override
    public GyzjFfjl queryLastInfoByGyzjId(Long gyzjId) {
        QueryWrapper<GyzjFfjl> queryWrapper = Wrappers.query();
        queryWrapper.eq("gyzj_id", gyzjId);
        queryWrapper.orderByDesc("id");
        return this.getOne(queryWrapper, false);
    }

    /**
     * 列表
     *
     * @param form 发放记录
     * @return 发放记录
     */
    @Override
    public List<FfjlListDto> queryList(FfjlSearchForm form) {
        QueryWrapper<GyzjFfjl> 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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是未锁定状态，不能加签");
        }
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是归档状态，不能借阅");
        }
        if (!gyzjFfjlOld.getJyzt().equals(JyztEnum.wjy.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是未借阅状态，不能借阅");
        }
        //申请借阅后，借阅状态更新为待借阅，等待档案员审核期间无法进行其他操作；
        gyzjFfjlOld.setJyzt(JyztEnum.djy.getValue());
        gyzjFfjlOld.setJyksrq(form.getStartDate());
        gyzjFfjlOld.setJyjsrq(form.getEndDate());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getStartDate()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getEndDate()));
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请借阅", JcmcysEnum.orange.getValue(), formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.gd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】未归档，不能解档");
        }
        //申请解档后，状态更新为待解档，等待档案员审核期间无法进行其他操作；
        gyzjFfjlOld.setJlzt(JlztEnum.djd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.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<GyzjFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<GyzjFfjl> gyzjFfjlList = this.list(queryWrapper);
        List<GyzjFfjlJcgj> gyzjFfjlJcgjList = new ArrayList<>();
        if (gyzjFfjlList.size() > 0) {
            for (GyzjFfjl gyzj : gyzjFfjlList) {
                if (!gyzj.getJlzt().equals(JlztEnum.ysd.getValue())) {
                    throw new ServiceException("发放回收记录【" + gyzj.getMc() + "】不是锁定状态，不能归档");
                }
            }
            for (GyzjFfjl gyzjFfjl : gyzjFfjlList) {
                //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
                gyzjFfjl.setJlzt(JlztEnum.dgd.getValue());
                gyzjFfjl.setDazt(DaztEnum.ygd.getValue());
                gyzjFfjl.setGdsqrId(qmr.getUserId());
                gyzjFfjl.setGdsqrMc(qmr.getNickName());
                // 稽查轨迹
                gyzjFfjlJcgjList.add(gyzjFfjlJcgjService.getJcgj(gyzjFfjl.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark()));
                //保存日志
                archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjl.getId(), gyzjFfjl.getMc(), gyzjFfjl.getBh(), gyzjFfjl.getNd()+gyzjFfjl.getNddw(), "申请归档");
            }
            this.updateBatchById(gyzjFfjlList);
            gyzjFfjlJcgjService.saveBatch(gyzjFfjlJcgjList);
        }
    }

    /**
     * 归档 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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是锁定状态，不能归档");
        }
        //申请归档，档案员可在档案管理中进行确认归档审核；申请后状态更新成待归档；
        gyzjFfjlOld.setJlzt(JlztEnum.dgd.getValue());
        gyzjFfjlOld.setDazt(DaztEnum.ygd.getValue());
        gyzjFfjlOld.setGdsqrId(qmr.getUserId());
        gyzjFfjlOld.setGdsqrMc(qmr.getNickName());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "申请归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "申请归档");
    }


    /**
     * 批量锁定 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<GyzjFfjl> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        queryWrapper.eq("jlzt", JlztEnum.wsd.getValue());
        List<GyzjFfjl> gyzjFfjlList = this.list(queryWrapper);
        List<GyzjFfjlJcgj> gyzjFfjlJcgjList = new ArrayList<>();
        if (gyzjFfjlList.size() <= 0) {
            throw new ServiceException("发放回收记录不是未锁定状态，不能锁定");
        }
        for (GyzjFfjl gyzjFfjl : gyzjFfjlList) {
            //锁定
            gyzjFfjl.setJlzt(JlztEnum.ysd.getValue());
            // 稽查轨迹
            gyzjFfjlJcgjList.add(gyzjFfjlJcgjService.getJcgj(gyzjFfjl.getId(), JcgjlxEnum.lc.getValue(), "锁定发放记录", JcmcysEnum.green.getValue(), null, qmr, form.getRemark()));
        }
        this.saveOrUpdateBatch(gyzjFfjlList);
        gyzjFfjlJcgjService.saveBatch(gyzjFfjlJcgjList);

    }

    /**
     * 锁定 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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.wsd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是未锁定状态，不能锁定");
        }
        gyzjFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.ysd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是已锁定状态，不能解锁");
        }
        //点击解锁，状态更新为入库；
        gyzjFfjlOld.setJlzt(JlztEnum.wsd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "解锁发放记录", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());

    }

    /**
     * 归还
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void gh(GhForm form, Gyzj gyzj, SysUser ghr1, SysUser ghr2, SysUser jsr1, SysUser jsr2) {
        GyzjFfjl gyzjFfjl = this.queryLastInfoByGyzjId(form.getId());
        if (gyzjFfjl == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        // 更新发放记录
        gyzjFfjl.setFfzytj(form.getGhzytj());
        gyzjFfjl.setRkwz(form.getRkwz());
        gyzjFfjl.setRktj(form.getRktj());
        gyzjFfjl.setGhzytj(form.getGhzytj());
        gyzjFfjl.setRkl(form.getRkl());
        gyzjFfjl.setRkldw(gyzj.getKcdw());
        gyzjFfjl.setRkmz(form.getRkmz());
        gyzjFfjl.setRkmzdw(gyzj.getKcdw());
        gyzjFfjl.setSyl(new BigDecimal(gyzjFfjl.getCkl()).subtract(new BigDecimal(gyzjFfjl.getRkl())).toString());
        gyzjFfjl.setSyldw(gyzj.getKcdw());
        gyzjFfjl.setGhr1Id(ghr1.getUserId());
        gyzjFfjl.setGhr1Mc(ghr1.getNickName());
        gyzjFfjl.setGhrq(new Date());
        gyzjFfjl.setGhr2Id(ghr2.getUserId());
        gyzjFfjl.setGhr2Mc(ghr2.getNickName());
        gyzjFfjl.setJsr1Id(jsr1.getUserId());
        gyzjFfjl.setJsr1Mc(jsr1.getNickName());
        gyzjFfjl.setJsr2Id(jsr2.getUserId());
        gyzjFfjl.setJsr2Mc(jsr2.getNickName());
        gyzjFfjl.setGhbz(form.getRemark());
        this.updateById(gyzjFfjl);

        // 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("入库位置", form.getRkwz());
        formData.put("入库条件", form.getRktj());
        formData.put("转移条件", form.getGhzytj());
        formData.put("入库净重", form.getRkl() + gyzj.getKcdw());
        formData.put("入库毛重", form.getRkmz() + gyzj.getKcdw());
//        formData.put("使用量", gyzjFfjl.getSyl() + gyzj.getKcdw());
        formData.put("归还人1", ghr1.getNickName());
        formData.put("归还人2", ghr2.getNickName());
        formData.put("库管员1", jsr1.getNickName());
        formData.put("库管员2", jsr2.getNickName());
        formData.put("备注", form.getRemark());
        formData.put("签名意义", "归还");
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjl.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), formData, SecurityUtils.getLoginUser().getSysUser(), form.getRemark());

    }


    /**
     * 发放
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ff(FfForm form, Gyzj gyzj, SysUser lqr1, SysUser lqr2, SysUser ffr1, SysUser ffr2, Date time) {

        //region 新增发放记录
        GyzjFfjl gyzjFfjl = new GyzjFfjl();
        gyzjFfjl.setGyzjId(gyzj.getId());
        gyzjFfjl.setMc(gyzj.getMc());
        gyzjFfjl.setBh(gyzj.getBh());
        gyzjFfjl.setNd(gyzj.getNd());
        gyzjFfjl.setNddw(gyzj.getNddw());
        gyzjFfjl.setCctj(gyzj.getCctj());
        gyzjFfjl.setCcwz(gyzj.getCcwz());
        gyzjFfjl.setSxrq(gyzj.getSxrq());
        gyzjFfjl.setCctj(gyzj.getCctj());
        gyzjFfjl.setCkl(gyzj.getKc());
        gyzjFfjl.setCkldw(gyzj.getKcdw());
        gyzjFfjl.setLqr1Id(lqr1.getUserId());
        gyzjFfjl.setLqr2Id(lqr2.getUserId());
        gyzjFfjl.setFfr1Id(ffr1.getUserId());
        gyzjFfjl.setFfr2Id(ffr2.getUserId());
        gyzjFfjl.setLqr1Mc(lqr1.getNickName());
        gyzjFfjl.setLqr2Mc(lqr2.getNickName());
        gyzjFfjl.setFfr1Mc(ffr1.getNickName());
        gyzjFfjl.setFfr2Mc(ffr2.getNickName());
        gyzjFfjl.setFfzytj(form.getZytj());
        gyzjFfjl.setFfrq(time);
        gyzjFfjl.setFfbz(form.getRemark());
        gyzjFfjl.setMdIds(form.getMdIds());
        gyzjFfjl.setMdOther(form.getMdOther());
        this.save(gyzjFfjl);
        //endregion

        //region 新增试验物资
        if (StringUtils.isNoneBlank(form.getMdIds())) {
            QueryWrapper<StudyGyzj> queryWrapper = Wrappers.query();
            queryWrapper.in("study_id", form.getMdIds());
            queryWrapper.in("resource_id", gyzj.getId());
            List<StudyGyzj> studyGyzjsOld = studyGyzjService.list(queryWrapper);
            List<StudyGyzj> studyGyzjs = new ArrayList<>();
            String[] tmp = form.getMdIds().split(",");
            for (String s : tmp) {
                List<StudyGyzj> existList = studyGyzjsOld.stream()
                        .filter(p -> p.getStudyId().equals(Long.parseLong(s)))
                        .collect(Collectors.toList());
                if (existList.size() == 0) {
                    StudyGyzj studyGyzj = new StudyGyzj();
                    studyGyzj.setStudyId(Long.parseLong(s));
                    studyGyzj.setResourceId(gyzj.getId());
                    studyGyzjs.add(studyGyzj);
                }
            }
            if (studyGyzjs.size() > 0) {
                studyGyzjService.insertBatch(studyGyzjs);
            }
        }
        //endregion

        //region 稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("出库量", gyzj.getKc() + gyzj.getKcdw());
        formData.put("转移条件", form.getZytj());
        formData.put("出库净重", gyzj.getKc() + gyzj.getKcdw());
        formData.put("目的", form.getMdMcs());
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjl.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<GyzjFfjl> 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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待归档状态，不能操作");
        }
        gyzjFfjlOld.setJlzt(JlztEnum.gd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意归档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "同意归档");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】同意归档",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.dgd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待归档状态，不能操作");
        }
        gyzjFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        gyzjFfjlOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝归档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "拒绝归档");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】拒绝归档",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待解档状态，不能操作");
        }
        gyzjFfjlOld.setJlzt(JlztEnum.ysd.getValue());
        gyzjFfjlOld.setDazt(DaztEnum.wgd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意解档", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "同意解档");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】同意解档",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJlzt().equals(JlztEnum.djd.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待解档状态，不能操作");
        }
        gyzjFfjlOld.setJlzt(JlztEnum.gd.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("原因", form.getRemark());
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝解档", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "拒绝解档");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】拒绝解档",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待借阅状态，不能操作");
        }
        gyzjFfjlOld.setJyzt(JyztEnum.jyz.getValue());
        gyzjFfjlOld.setJyksrq(form.getJyksrq());
        gyzjFfjlOld.setJyjsrq(form.getJyjsrq());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "同意借阅", JcmcysEnum.green.getValue(), formData, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "同意借阅");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】同意借阅",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJyzt().equals(JyztEnum.djy.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是待借阅状态，不能操作");
        }
        gyzjFfjlOld.setJyzt(JyztEnum.wjy.getValue());
        gyzjFfjlOld.setJyksrq(form.getJyksrq());
        gyzjFfjlOld.setJyjsrq(form.getJyjsrq());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        Map<String, String> formData = new LinkedHashMap<>();
        formData.put("借阅开始时间", DateUtils.dateTime(form.getJyksrq()));
        formData.put("借阅结束时间", DateUtils.dateTime(form.getJyjsrq()));
        formData.put("原因", form.getRemark());
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "拒绝借阅", JcmcysEnum.green.getValue(), formData, qmr, null);
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "拒绝借阅");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】拒绝借阅",gyzjFfjlOld.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不正确");
        }
        GyzjFfjl gyzjFfjlOld = this.getById(form.getId());
        if (gyzjFfjlOld == null) {
            throw new ServiceException("发放回收记录不存在或已删除");
        }
        if (!gyzjFfjlOld.getJyzt().equals(JyztEnum.jyz.getValue())) {
            throw new ServiceException("发放回收记录【" + gyzjFfjlOld.getMc() + "】不是借阅中状态，不能操作");
        }
        gyzjFfjlOld.setJyzt(JyztEnum.wjy.getValue());
        this.updateById(gyzjFfjlOld);
        //稽查轨迹
        gyzjFfjlJcgjService.saveJcgj(gyzjFfjlOld.getId(), JcgjlxEnum.lc.getValue(), "确认归还", JcmcysEnum.green.getValue(), null, qmr, form.getRemark());
        //保存日志
        archiveLogService.saveInfo(ArchiveLogTypeEnum.gyzjlqff, gyzjFfjlOld.getId(), gyzjFfjlOld.getMc(), gyzjFfjlOld.getBh(), gyzjFfjlOld.getNd()+gyzjFfjlOld.getNddw(), "确认归还");
        //通知
        noticeService.save("发放回收记录【"+gyzjFfjlOld.getMc()+"】确认归还",gyzjFfjlOld.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, false);
    }

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