package com.hxhq.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hxhq.common.core.domain.MpBaseEntity;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysUser;
import org.springframework.stereotype.Service;
import com.hxhq.business.mapper.YqJcgjMapper;
import com.hxhq.business.domain.YqJcgj;
import com.hxhq.business.service.IYqJcgjService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 仪器管理-稽查轨迹Service业务层处理
 *
 * @author HanLong
 * @date 2025-12-22
 */
@Service
public class YqJcgjServiceImpl extends ServiceImpl<YqJcgjMapper, YqJcgj> implements IYqJcgjService
{

    /**
     * 查询仪器管理-稽查轨迹列表
     *
     * @param yqJcgj 仪器管理-稽查轨迹
     * @return 仪器管理-稽查轨迹
     */
    @Override
    public List<YqJcgj> queryList(YqJcgj yqJcgj)
    {
        LambdaQueryWrapper<YqJcgj> queryWrapper = new LambdaQueryWrapper<>();
        if (yqJcgj.getYqId() == null || yqJcgj.getYqId().longValue() < 0) {
            throw new SecurityException("仪器id不能为空");
        }
        queryWrapper.eq(YqJcgj::getYqId, yqJcgj.getYqId());
        if (yqJcgj.getJcgjlx() != null && yqJcgj.getJcgjlx().intValue() > 0) {
            queryWrapper.eq(YqJcgj::getJcgjlx, yqJcgj.getJcgjlx());
        }
        if (StringUtils.isNoneBlank(yqJcgj.getJcmc())) {
            queryWrapper.and(p -> p.like(YqJcgj::getJcmc, yqJcgj.getJcmc())
                    .or().like(YqJcgj::getJcgjlx, yqJcgj.getJcmc()));
        }
        queryWrapper.orderByDesc(MpBaseEntity::getId);
        return this.list(queryWrapper);
    }

    @Override
    public void saveJcgj(Long  yqId, Integer jcgjlx, String jcmc, String jcmcEn, Integer jcmcys, String jcnr, String jcnrEn) {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        YqJcgj yqJcgj = new YqJcgj();
        yqJcgj.setYqId(yqId);
        yqJcgj.setJcgjlx(jcgjlx);
        yqJcgj.setJcmc(jcmc);
        yqJcgj.setJcmcEn(jcmcEn);
        yqJcgj.setJcmcys(jcmcys);
        yqJcgj.setJcnr(jcnr);
        yqJcgj.setJcnrEn(jcnrEn);
        yqJcgj.setQmrId(sysUser.getUserId());
        yqJcgj.setQmrMc(sysUser.getNickName());
        yqJcgj.setQmrMcEn(sysUser.getUserName());
        this.save(yqJcgj);
    }

}
