package vip.xiaonuo.quality.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.quality.entity.QualityTarget;
import vip.xiaonuo.quality.mapper.QualityTargetMapper;
import vip.xiaonuo.quality.param.QualityParam;
import vip.xiaonuo.quality.service.IQualityTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 质量管控指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-11
 */
@Service
public class QualityTargetServiceImpl extends ServiceImpl<QualityTargetMapper, QualityTarget> implements IQualityTargetService {

    @Autowired
    private QualityTargetMapper targetMapper;

    @Override
    public QualityTarget getTargetOne(QualityParam qualityParam) {
        String orgId = qualityParam.getOrgId();
        QueryWrapper<QualityTarget> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.orderByDesc("id").last(" limit 1");
        QualityTarget qualityTarget = targetMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(qualityTarget)){
            qualityTarget =   new QualityTarget();
            qualityTarget.setCreateTime(new Date());
            qualityTarget.setDefectiveRate("0");
            qualityTarget.setProduceOneRate("0");
            qualityTarget.setProduceRate("0");
            qualityTarget.setUnqualifiedBatch("0");
        }
        return qualityTarget;
    }
}
