package com.hxhq.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hxhq.business.utils.lang.MjyJcnrUtil;
import com.hxhq.common.core.exception.ServiceException;
import com.hxhq.system.api.domain.SysUser;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.MjyTzMapper;
import com.hxhq.business.domain.MjyTz;
import com.hxhq.business.service.IMjyTzService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 麻精药-台账Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-18
 */
@Service
public class MjyTzServiceImpl extends ServiceImpl<MjyTzMapper, MjyTz> implements IMjyTzService {

    /**
     * 查询台账列表
     *
     * @param mjyTz 麻精药
     * @return
     */
    @Override
    public List<MjyTz> queryList(MjyTz mjyTz) {
        QueryWrapper<MjyTz> queryWrapper = Wrappers.query();
        if (mjyTz.getMjyId() == null || mjyTz.getMjyId().longValue() < 0) {
            throw new ServiceException("麻精药id不能为空");
        }
        queryWrapper.eq("mjy_id", mjyTz.getMjyId());
        queryWrapper.orderByDesc("id");
        return baseMapper.queryList(queryWrapper);
    }

    /**
     * 新增台账
     *
     * @param mjyId  麻精药id
     * @param lqr1   领取人1
     * @param lqr2   领取人2
     * @param ghr1   归还人1
     * @param ghr2   归还人2
     * @param ffr1   发放人1
     * @param ffr2   发放人2
     * @param qmr    签名人
     * @param qmyy   签名意义
     * @param czl    操作量
     * @param czldw  操作量单位
     * @param remark 备注
     */
    @Override
    public void saveTz(Long mjyId, SysUser lqr1, SysUser lqr2, SysUser ghr1, SysUser ghr2, SysUser ffr1, SysUser ffr2, SysUser jsr1, SysUser jsr2, SysUser qmr, String qmyy, String czl, String czldw, String remark) {
        MjyTz mjyTz = new MjyTz();
        mjyTz.setMjyId(mjyId);
        if (lqr1 != null && lqr2 != null) {
            mjyTz.setLqrMc(lqr1.getNickName() +","+ lqr2.getNickName());
            mjyTz.setLqrMcEn(lqr1.getUserName() +","+ lqr2.getUserName());
        }

        if (ghr1 != null && ghr2 != null) {
            mjyTz.setGhrMc(ghr1.getNickName() +","+ ghr2.getNickName());
            mjyTz.setGhrMcEn(ghr1.getUserName() +","+ ghr2.getUserName());
        }
        if (ffr1 != null && ffr2 != null) {
            mjyTz.setFfrMc(ffr1.getNickName() +","+ ffr2.getNickName());
            mjyTz.setFfrMcEn(ffr1.getUserName() +","+ ffr2.getUserName());
        }
        if (jsr1 != null && jsr2 != null) {
            mjyTz.setJsrMc(jsr1.getNickName() +","+ jsr2.getNickName());
            mjyTz.setJsrMcEn(jsr1.getUserName() +","+ jsr2.getUserName());
        }
        if (qmr != null && qmr != null) {
            mjyTz.setQmrId(qmr.getUserId());
            mjyTz.setQmrMc(qmr.getNickName());
            mjyTz.setQmrMcEn(qmr.getUserName());
        }
        mjyTz.setQmyy(qmyy);
        mjyTz.setQmyyEn(MjyJcnrUtil.getEn(qmyy));
        mjyTz.setCzldw(czldw);
        mjyTz.setCzl(czl);
        mjyTz.setRemark(remark);
        this.save(mjyTz);
    }

    /**
     * 新增台账
     *
     * @param mjyId  麻精药id
     * @param lqr1   领取人1
     * @param lqr2   领取人2
     * @param ghr1   归还人1
     * @param ghr2   归还人2
     * @param ffr1   发放人1
     * @param ffr2   发放人2
     * @param qmr    签名人
     * @param qmyy   签名意义
     * @param czl    操作量
     * @param czldw  操作量单位
     * @param remark 备注
     */
    @Override
    public MjyTz getTz(Long mjyId, SysUser lqr1, SysUser lqr2, SysUser ghr1, SysUser ghr2, SysUser ffr1, SysUser ffr2, SysUser jsr1, SysUser jsr2, SysUser qmr, String qmyy, String czl, String czldw, String remark) {
        MjyTz mjyTz = new MjyTz();
        mjyTz.setMjyId(mjyId);
        if (lqr1 != null && lqr2 != null) {
            mjyTz.setLqrMc(lqr1.getNickName() +","+ lqr2.getNickName());
            mjyTz.setLqrMcEn(lqr1.getUserName() +","+ lqr2.getUserName());
        }

        if (ghr1 != null && ghr2 != null) {
            mjyTz.setGhrMc(ghr1.getNickName() +","+ ghr2.getNickName());
            mjyTz.setGhrMcEn(ghr1.getUserName() +","+ ghr2.getUserName());
        }
        if (ffr1 != null && ffr2 != null) {
            mjyTz.setFfrMc(ffr1.getNickName() +","+ ffr2.getNickName());
            mjyTz.setFfrMcEn(ffr1.getUserName() +","+ ffr2.getUserName());
        }
        if (jsr1 != null && jsr2 != null) {
            mjyTz.setJsrMc(jsr1.getNickName() +","+ jsr2.getNickName());
            mjyTz.setJsrMcEn(jsr1.getUserName() +","+ jsr2.getUserName());
        }
        if (qmr != null && qmr != null) {
            mjyTz.setQmrId(qmr.getUserId());
            mjyTz.setQmrMc(qmr.getNickName());
            mjyTz.setQmrMcEn(qmr.getUserName());
        }
        mjyTz.setQmyy(qmyy);
        mjyTz.setQmyyEn(MjyJcnrUtil.getEn(qmyy));
        mjyTz.setCzldw(czldw);
        mjyTz.setCzl(czl);
        mjyTz.setRemark(remark);
        return mjyTz;
    }

    /**
     * 新增台账
     *
     * @param mjyId  麻精药id
     * @param lqr1   领取人1
     * @param lqr2   领取人2
     * @param ghr1   归还人1
     * @param ghr2   归还人2
     * @param ffr1   发放人1
     * @param ffr2   发放人2
     * @param qmr    签名人
     * @param qmyy   签名意义
     * @param czl    操作量
     * @param czldw  操作量单位
     * @param remark 备注
     * @param bdId 表单id
     */
    @Override
    public MjyTz getTzGen(Long mjyId, SysUser lqr1, SysUser lqr2, SysUser ghr1, SysUser ghr2, SysUser ffr1, SysUser ffr2, SysUser jsr1, SysUser jsr2, SysUser qmr, String qmyy, String czl, String czldw, String remark,Long bdId) {
        MjyTz mjyTz = new MjyTz();
        mjyTz.setMjyId(mjyId);
        if (lqr1 != null && lqr2 != null) {
            mjyTz.setLqrMc(lqr1.getNickName() +","+ lqr2.getNickName());
            mjyTz.setLqrMcEn(lqr1.getUserName() +","+ lqr2.getUserName());
        }

        if (ghr1 != null && ghr2 != null) {
            mjyTz.setGhrMc(ghr1.getNickName() +","+ ghr2.getNickName());
            mjyTz.setGhrMcEn(ghr1.getUserName() +","+ ghr2.getUserName());
        }
        if (ffr1 != null && ffr2 != null) {
            mjyTz.setFfrMc(ffr1.getNickName() +","+ ffr2.getNickName());
            mjyTz.setFfrMcEn(ffr1.getUserName() +","+ ffr2.getUserName());
        }
        if (jsr1 != null && jsr2 != null) {
            mjyTz.setJsrMc(jsr1.getNickName() +","+ jsr2.getNickName());
            mjyTz.setJsrMcEn(jsr1.getUserName() +","+ jsr2.getUserName());
        }
        if (qmr != null && qmr != null) {
            mjyTz.setQmrId(qmr.getUserId());
            mjyTz.setQmrMc(qmr.getNickName());
            mjyTz.setQmrMcEn(qmr.getUserName());
        }
        mjyTz.setQmyy(qmyy);
        mjyTz.setQmyyEn(MjyJcnrUtil.getEn(qmyy));
        mjyTz.setCzldw(czldw);
        mjyTz.setCzl(czl);
        mjyTz.setBdId(bdId);
        mjyTz.setRemark(remark);
        return mjyTz;
    }
}
