package com.hxhq.business.service.impl;

import java.util.List;
import java.util.Map;

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

/**
 * 麻精药发放记录-稽查轨迹Service业务层处理
 *
 * @author hxhq
 * @date 2025-12-25
 */
@Service
public class MjyFfjlJcgjServiceImpl extends ServiceImpl<MjyFfjlJcgjMapper, MjyFfjlJcgj> implements IMjyFfjlJcgjService
{

    /**
     * 查询稽查轨迹列表
     * @param mjyFfjlJcgj 麻给药
     * @return
     */
    @Override
    public List<MjyFfjlJcgj> queryList(MjyFfjlJcgj mjyFfjlJcgj){
        QueryWrapper<MjyFfjlJcgj> queryWrapper = Wrappers.query();
        if(mjyFfjlJcgj.getFfjlId()==null||mjyFfjlJcgj.getFfjlId().longValue()<0){
            throw new ServiceException("发放记录id不能为空");
        }
        queryWrapper.eq("ffjl_id",mjyFfjlJcgj.getFfjlId());
        if(mjyFfjlJcgj.getJcgjlx()!=null&&mjyFfjlJcgj.getJcgjlx().intValue()>0){
            queryWrapper.eq("jcgjlx",mjyFfjlJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(mjyFfjlJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like("`jcmc`", mjyFfjlJcgj.getJcmc())
                    .or().like("`jcnr`", mjyFfjlJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

    /**
     * 新增稽查轨迹
     * @param ffjlId 发放记录id
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public  void saveJcgj(Long  ffjlId, Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr,SysUser qmr,String remark){
        MjyFfjlJcgj mjyFfjlJcgj = new MjyFfjlJcgj();
        mjyFfjlJcgj.setFfjlId(ffjlId);
        mjyFfjlJcgj.setJcgjlx(jcgjlx);
        mjyFfjlJcgj.setJcmc(jcmc);
        mjyFfjlJcgj.setJcmcEn(MjyJcnrUtil.getEn(jcmc));
        mjyFfjlJcgj.setJcmcys(jcmcys);
        if(jcnr!=null){
            mjyFfjlJcgj.setJcnr(JctUtil.formatStr(jcnr));
            mjyFfjlJcgj.setJcnrEn(MjyJcnrUtil.getJcnrEn(jcnr));
        }
        mjyFfjlJcgj.setQmrId(qmr.getUserId());
        mjyFfjlJcgj.setQmrMc(qmr.getNickName());
        mjyFfjlJcgj.setQmrMcEn(qmr.getUserName());
        mjyFfjlJcgj.setRemark(remark);
        this.save(mjyFfjlJcgj);
    }

    /**
     * 新增稽查轨迹
     * @param ffjlId 发放记录id
     * @param jcgjlx 稽查轨迹类型:1:流程；3：编辑
     * @param jcmc 稽查名称
     * @param jcmcys 稽查名称颜色：1：蓝色；3：红色；5：绿色；7：橙色
     * @param jcnr 稽查内容
     * @param qmr 签名人
     * @param remark 备注
     */
    @Override
    public  MjyFfjlJcgj getJcgj(Long  ffjlId, Integer jcgjlx, String jcmc, Integer jcmcys, Map<String, String> jcnr,SysUser qmr,String remark){
        MjyFfjlJcgj mjyFfjlJcgj = new MjyFfjlJcgj();
        mjyFfjlJcgj.setFfjlId(ffjlId);
        mjyFfjlJcgj.setJcgjlx(jcgjlx);
        mjyFfjlJcgj.setJcmc(jcmc);
        mjyFfjlJcgj.setJcmcEn(MjyJcnrUtil.getEn(jcmc));
        mjyFfjlJcgj.setJcmcys(jcmcys);
        if(jcnr!=null){
            mjyFfjlJcgj.setJcnr(JctUtil.formatStr(jcnr));
            mjyFfjlJcgj.setJcnrEn(MjyJcnrUtil.getJcnrEn(jcnr));
        }
        mjyFfjlJcgj.setQmrId(qmr.getUserId());
        mjyFfjlJcgj.setQmrMc(qmr.getNickName());
        mjyFfjlJcgj.setQmrMcEn(qmr.getUserName());
        mjyFfjlJcgj.setRemark(remark);
        return mjyFfjlJcgj;
    }


}
