package com.hxhq.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hxhq.business.domain.GyzjFfjlJcgj;
import com.hxhq.business.mapper.GyzjFfjlJcgjMapper;
import com.hxhq.business.service.IGyzjFfjlJcgjService;
import com.hxhq.business.utils.JctUtil;
import com.hxhq.business.utils.lang.GyzjJcnrUtil;
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 java.util.List;
import java.util.Map;

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

    /**
     * 查询稽查轨迹列表
     * @param gyzjFfjlJcgj 麻给药
     * @return
     */
    @Override
    public List<GyzjFfjlJcgj> queryList(GyzjFfjlJcgj gyzjFfjlJcgj){
        QueryWrapper<GyzjFfjlJcgj> queryWrapper = Wrappers.query();
        if(gyzjFfjlJcgj.getFfjlId()==null||gyzjFfjlJcgj.getFfjlId().longValue()<0){
            throw new ServiceException("发放记录id不能为空");
        }
        queryWrapper.eq("ffjl_id",gyzjFfjlJcgj.getFfjlId());
        if(gyzjFfjlJcgj.getJcgjlx()!=null&&gyzjFfjlJcgj.getJcgjlx().intValue()>0){
            queryWrapper.eq("jcgjlx",gyzjFfjlJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(gyzjFfjlJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like("`jcmc`", gyzjFfjlJcgj.getJcmc())
                    .or().like("`jcnr`", gyzjFfjlJcgj.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){
        GyzjFfjlJcgj gyzjFfjlJcgj = new GyzjFfjlJcgj();
        gyzjFfjlJcgj.setFfjlId(ffjlId);
        gyzjFfjlJcgj.setJcgjlx(jcgjlx);
        gyzjFfjlJcgj.setJcmc(jcmc);
        gyzjFfjlJcgj.setJcmcEn(GyzjJcnrUtil.getEn(jcmc));
        gyzjFfjlJcgj.setJcmcys(jcmcys);
        if(jcnr!=null){
            gyzjFfjlJcgj.setJcnr(JctUtil.formatStr(jcnr));
            gyzjFfjlJcgj.setJcnrEn(GyzjJcnrUtil.getJcnrEn(jcnr));
        }
        gyzjFfjlJcgj.setQmrId(qmr.getUserId());
        gyzjFfjlJcgj.setQmrMc(qmr.getNickName());
        gyzjFfjlJcgj.setQmrMcEn(qmr.getUserName());
        gyzjFfjlJcgj.setRemark(remark);
        this.save(gyzjFfjlJcgj);
    }

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


}
