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.purchase.entity.PurchaseOrder;
import vip.xiaonuo.purchase.mapper.PurchaseOrderMapper;
import vip.xiaonuo.quality.entity.QualityTarget;
import vip.xiaonuo.quality.entity.QualityWrite;
import vip.xiaonuo.quality.mapper.QualityTargetMapper;
import vip.xiaonuo.quality.mapper.QualityWriteMapper;
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 vip.xiaonuo.write.entity.WriteProduct;
import vip.xiaonuo.write.mapper.WriteProductMapper;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private QualityTargetMapper targetMapper;
    @Autowired
    private WriteProductMapper writeProductMapper;
    @Autowired
    private QualityWriteMapper qualityWriteMapper;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public QualityTarget getTargetOne(QualityParam qualityParam) {
        String orgId = qualityParam.getOrgId();
        String time = qualityParam.getTime();
        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");
        }
        QueryWrapper<QualityWrite> qualityWriteQueryWrapper = new QueryWrapper<>();
        qualityWriteQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        qualityWriteQueryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        Long count = qualityWriteMapper.selectCount(qualityWriteQueryWrapper);//不合格批次

        QueryWrapper<PurchaseOrder> orderQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        orderQueryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        Long aLong = purchaseOrderMapper.selectCount(orderQueryWrapper);//合格批次

        QueryWrapper<WriteProduct> writeProductQueryWrapper = new QueryWrapper<>();
        orderQueryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        orderQueryWrapper.between("report_date", time + "-01 00:00:00", time + "-31 23:59:59");
        List<WriteProduct> writeProducts = writeProductMapper.selectList(writeProductQueryWrapper);
        Double countNumber = 0.00;//总数
        Double count1 = 0.00;//检测合格数量
        if (writeProducts.size() != 0) {
            for (WriteProduct writeProduct : writeProducts) {
                countNumber = countNumber + Double.valueOf(writeProduct.getProductionNumber());
                if (writeProduct.getIsQualified() == 0) {
                    count1 = count1 + Double.valueOf(writeProduct.getProductionNumber());
                }
            }
        }

        DecimalFormat def = new DecimalFormat("0.00");

        qualityTarget.setUnqualifiedBatch(count.toString());
        long batch = count + aLong;
        if (0 == batch) {
            qualityTarget.setDefectiveRate("0");
        } else {
            Double aDouble = Double.valueOf(count / batch);
            qualityTarget.setDefectiveRate(def.format(aDouble * 100));
        }
        if (0.00 == countNumber) {
            qualityTarget.setProduceOneRate("100");
            qualityTarget.setProduceRate("100");
        } else {
            double v = count1 / countNumber;
            qualityTarget.setProduceOneRate(def.format(v * 100));
            qualityTarget.setProduceRate(def.format(v * 100));
        }
        return qualityTarget;
    }
}
