package com.hxhq.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.domain.ZcgTz;
import com.hxhq.business.enums.zykgl.zcgJyztEnum;
import com.hxhq.business.form.zcg.ZcgYsffForm;
import com.hxhq.business.form.zcg.ZcgYsghForm;
import com.hxhq.business.form.zcg.ZcgSearchForm;
import com.hxhq.business.service.IZcgTzService;
import com.hxhq.common.core.exception.ServiceException;
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.ZcgMapper;
import com.hxhq.business.domain.Zcg;
import com.hxhq.business.service.IZcgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 暂存柜Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-18
 */
@Service
public class ZcgServiceImpl extends ServiceImpl<ZcgMapper, Zcg> implements IZcgService {
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IZcgTzService zcgTzService;

    /**
     * 查询暂存柜列表
     *
     * @param form 暂存柜
     * @return 暂存柜
     */
    @Override
    public List<Zcg> queryList(ZcgSearchForm form) {
        QueryWrapper<Zcg> queryWrapper = Wrappers.query();
        queryWrapper.eq("t.del_flag", "0");
        if (form.getZt() != null && form.getZt().intValue() > 0) {
            queryWrapper.eq("t.zt", form.getZt());
        }
        if (StringUtils.isNoneBlank(form.getMc())) {
            queryWrapper.and(p -> p.like("t.`mc`", form.getMc()));
        }
        if (form.getJyrId() != null && form.getJyrId().intValue() > 0) {
            queryWrapper.and(p -> p.eq("t.`lqr1_id`", form.getJyrId()).or().eq("t.`lqr2_id`", form.getJyrId()));
        }
        queryWrapper.orderByDesc("t.create_time");
        return baseMapper.queryList(queryWrapper);
    }


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


    /**
     * 钥匙归还
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ysgh(ZcgYsghForm form) {
        SysUser ghr1 = sysUserService.selectUserById(form.getGhr1Id());
        if (ghr1 == null) {
            throw new SecurityException("归还人1不存在");
        }
        SysUser ghr2 = sysUserService.selectUserById(form.getGhr2Id());
        if (ghr2 == null) {
            throw new SecurityException("归还人2不存在");
        }
        //验证接收人密码
        checkPassword(SecurityUtils.getLoginUser().getSysUser(),form.getJsrmm(),true);
        //验证钥匙1归还人密码
        checkPassword(ghr1,form.getGhr1mm(),true);
        //验证钥匙2归还人密码
        checkPassword(ghr2,form.getGhr2mm(),true);

        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Zcg zcgOld = this.getById(form.getId());
        if (zcgOld == null) {
            throw new SecurityException("暂存柜不存在或已删除");
        }
        if (!zcgOld.getZt().equals(zcgJyztEnum.jyz.getValue())) {
            throw new SecurityException("暂存柜未借用");
        }
        zcgOld.setLqr1Id(null);
        zcgOld.setLqr1Mc(null);
        zcgOld.setLqr1McEn(null);
        zcgOld.setLqr2Id(null);
        zcgOld.setLqr2Mc(null);
        zcgOld.setLqr2McEn(null);
        zcgOld.setGlyj(null);
        zcgOld.setZt(zcgJyztEnum.wjy.getValue());
        this.updateById(zcgOld);
    }


    /**
     * 批量钥匙归还
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ysghBatch(ZcgYsghForm form) {
        SysUser ghr1 = sysUserService.selectUserById(form.getGhr1Id());
        if (ghr1 == null) {
            throw new SecurityException("归还人1不存在");
        }
        SysUser ghr2 = sysUserService.selectUserById(form.getGhr2Id());
        if (ghr2 == null) {
            throw new SecurityException("归还人2不存在");
        }
        //验证接收人密码
        checkPassword(SecurityUtils.getLoginUser().getSysUser(),form.getJsrmm(),true);
        //验证钥匙1归还人密码
        checkPassword(ghr1,form.getGhr1mm(),true);
        //验证钥匙2归还人密码
        checkPassword(ghr2,form.getGhr2mm(),true);
        if (form.getIds().length<0) {
            throw new SecurityException("参数ids不正确");
        }
        QueryWrapper<Zcg> queryWrapper = Wrappers.query();
        queryWrapper.in("id", form.getIds());
        List<Zcg> zcgList = this.list(queryWrapper);
        if (zcgList.size() > 0) {
            for (Zcg zcg : zcgList) {
                if (!zcg.getZt().equals(zcgJyztEnum.jyz.getValue())) {
                    throw new SecurityException("暂存柜" + zcg.getMc() + "未借用");
                }
            }
            for (Zcg zcg : zcgList) {
                zcg.setLqr1Id(null);
                zcg.setLqr1Mc(null);
                zcg.setLqr1McEn(null);
                zcg.setLqr2Id(null);
                zcg.setLqr2Mc(null);
                zcg.setLqr2McEn(null);
                zcg.setGlyj(null);
                zcg.setZt(zcgJyztEnum.wjy.getValue());
            }
            this.updateBatchById(zcgList);
        }

    }

    /**
     * 钥匙发放
     *
     * @param form
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void ysff(ZcgYsffForm form) {
        //获取领取人信息
        SysUser lqr1 = sysUserService.selectUserById(form.getLqr1Id());
        if (lqr1 == null) {
            throw new SecurityException("领取人1不存在");
        }
        SysUser lqr2 = sysUserService.selectUserById(form.getLqr2Id());
        if (lqr2 == null) {
            throw new SecurityException("领取人2不存在");
        }
        //验证发放人密码
        checkPassword(SecurityUtils.getLoginUser().getSysUser(),form.getFfrmm(),true);
        //验证钥匙1领取人密码
        checkPassword(lqr1,form.getLqr1mm(),true);
        //验证钥匙2领取人密码
        checkPassword(lqr2,form.getLqr2mm(),true);
        if (form.getId() == null || form.getId().longValue() < 0) {
            throw new SecurityException("参数id不正确");
        }
        Zcg zcgOld = this.getById(form.getId());
        if (zcgOld == null) {
            throw new SecurityException("暂存柜不存在或已删除");
        }
        if (!zcgOld.getZt().equals(zcgJyztEnum.wjy.getValue())) {
            throw new SecurityException("暂存柜已借用");
        }
        zcgOld.setLqr1Id(form.getLqr1Id());
        zcgOld.setLqr1Mc(lqr1.getNickName());
        zcgOld.setLqr1McEn(lqr1.getUserName());
        zcgOld.setLqr2Id(form.getLqr2Id());
        zcgOld.setLqr2Mc(lqr2.getNickName());
        zcgOld.setLqr2McEn(lqr2.getUserName());
        if (StringUtils.isBlank(zcgOld.getGlyj())) {
            zcgOld.setGlyj(form.getGlyj());
        } else {
            zcgOld.setGlyj(zcgOld.getGlyj() + "，" + form.getGlyj());
        }
        zcgOld.setZt(zcgJyztEnum.jyz.getValue());
        this.updateById(zcgOld);
    }


    /**
     * 验证密码
     * @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);
    }

}
