package vip.xiaonuo.production.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.production.entity.ProductionEnergy;
import vip.xiaonuo.production.entity.ProductionTarget;
import vip.xiaonuo.production.entity.ProductionWorkstopPlan;
import vip.xiaonuo.production.entity.ProductionWritePlan;
import vip.xiaonuo.production.mapper.ProductionTargetMapper;
import vip.xiaonuo.production.mapper.ProductionWorkstopPlanMapper;
import vip.xiaonuo.production.mapper.ProductionWriteEnergyMapper;
import vip.xiaonuo.production.mapper.ProductionWritePlanMapper;
import vip.xiaonuo.production.param.ProductionParam;
import vip.xiaonuo.production.service.IProductionTargetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * <p>
 * 生产管控指标 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionTargetServiceImpl extends ServiceImpl<ProductionTargetMapper, ProductionTarget> implements IProductionTargetService {

    @Autowired
    private ProductionTargetMapper targetMapper;
    @Autowired
    private ProductionWorkstopPlanMapper planMapper;
    @Autowired
    private ProductionWritePlanMapper writePlanMapper;


    @Override
    public ProductionTarget getTarget(ProductionParam productionParam) {
        String orgId = productionParam.getOrgId();
        String time = productionParam.getTime();
        String produceName = productionParam.getProduceName();
        String locationName = productionParam.getLocationName();
        QueryWrapper<ProductionWorkstopPlan> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        queryWrapper.select("Convert(IFNULL(sum(actual_production),'0'),decimal(10,2)) actualProduction");
        if (StringUtils.isNotBlank(produceName)) {
            queryWrapper.like("produce_name", produceName);
        }
        queryWrapper.eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
        ProductionWorkstopPlan workstopPlan = planMapper.selectOne(queryWrapper);//实际产量

        QueryWrapper<ProductionWritePlan> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper1.eq(StringUtils.isNotBlank(locationName), "location_name", locationName);
        queryWrapper1.eq(StringUtils.isNotBlank(produceName), "production_name", produceName);
        queryWrapper1.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
        queryWrapper1.select(" Convert(IFNULL(sum(production_plan),'0'),decimal(10,2)) productionPlan");
        ProductionWritePlan writePlan = writePlanMapper.selectOne(queryWrapper1);//计划生产量

        ProductionTarget productionTarget = new ProductionTarget();
         if (ObjectUtil.isEmpty(writePlan) || "0.00".equals(writePlan.getProductionPlan())) {
            productionTarget.setPlanProduction("0");
            productionTarget.setPlanCompleteRate("0");
        } else {
            productionTarget.setPlanProduction(writePlan.getProductionPlan());
            double v = Double.valueOf(workstopPlan.getActualProduction()) / Double.valueOf(writePlan.getProductionPlan());
            if (v > 1.0) {
                productionTarget.setPlanCompleteRate("100");
            } else {
                DecimalFormat def = new DecimalFormat("0.00");
                productionTarget.setPlanCompleteRate(def.format(v * 100));
            }
        }

        productionTarget.setActualProduction(workstopPlan.getActualProduction());
        productionTarget.setReceiveRate("0");
        productionTarget.setProductionEnergy("0");

        return productionTarget;
    }

}
