package vip.xiaonuo.production.service.impl;

import cn.hutool.core.date.DateUtil;
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.erp.entity.ErpBdMaterial;
import vip.xiaonuo.erp.mapper.ErpBdMaterialMapper;
import vip.xiaonuo.production.entity.ProductionWritePlan;
import vip.xiaonuo.production.entity.ProductionYield;
import vip.xiaonuo.production.mapper.ProductionWritePlanMapper;
import vip.xiaonuo.production.mapper.ProductionYieldMapper;
import vip.xiaonuo.production.param.ProductionYieldParam;
import vip.xiaonuo.production.service.IProductionYieldService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.lang.reflect.WildcardType;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 生成管控产量情况 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionYieldServiceImpl extends ServiceImpl<ProductionYieldMapper, ProductionYield> implements IProductionYieldService {

    @Autowired
    private ProductionYieldMapper yieldMapper;
    @Autowired
    private ProductionWritePlanMapper writePlanMapper;

    @Autowired
    private ErpBdMaterialMapper erpBdMaterialMapper;

    @Override
    public List<ProductionYield> getList(ProductionYieldParam yieldParam) {
        String orgId = yieldParam.getOrgId();
        Integer timeType = yieldParam.getTimeType();
        String locationName = yieldParam.getLocationName();
        String produceName = yieldParam.getProduceName();
        Integer number = yieldParam.getNumber();
        QueryWrapper<ProductionYield> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId)
                .eq(StringUtils.isNotBlank(locationName),"location_name",locationName);
        if (StringUtils.isNotBlank(produceName)) {
            queryWrapper.like(StringUtils.isNotBlank(produceName), "produce_name", produceName);
        }
        DecimalFormat df = new DecimalFormat("#.00");
        queryWrapper.select(" convert(IFNULL(sum(actual_production),'0'),decimal(10,2)) actualProduction,create_time createTime");
        if (0 == timeType) {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.YEAR, -3);
            } else {
                calendar.add(Calendar.YEAR, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y')");
        } else {
            Calendar calendar = Calendar.getInstance();
            if (null == number) {
                calendar.add(Calendar.MONTH, -13);
            } else {
                calendar.add(Calendar.MONTH, -number);
            }
            queryWrapper.between("create_time", calendar.getTime(), new Date());
            queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
        }
        queryWrapper.orderByAsc("create_time");
        List<ProductionYield> productionYields = yieldMapper.selectList(queryWrapper);

        for (ProductionYield productionYield : productionYields) {

            List<ErpBdMaterial> erpBdMaterials = erpBdMaterialMapper.selectList(new QueryWrapper<ErpBdMaterial>()
                    .eq(StringUtils.isNotBlank(produceName), "fname", produceName)
                    .eq(StringUtils.isNotBlank(orgId), "org_id", orgId));
            String conver = "1";
            if (ObjectUtil.isNotEmpty(erpBdMaterials) && StringUtils.isNotBlank(erpBdMaterials.get(0).getConver() )){
                conver= erpBdMaterials.get(0).getConver();
            }


            QueryWrapper<ProductionWritePlan> queryWrapper1 = new QueryWrapper<>();
            if (0 == timeType) {
                Calendar calendar = Calendar.getInstance();
                if (null == number) {
                    calendar.add(Calendar.MONTH, -13);
                } else {
                    calendar.add(Calendar.MONTH, -number);
                }
                queryWrapper1.between("create_time", calendar.getTime(), new Date());
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
//                String format = sdf.format(productionYield.getCreateTime());
//                queryWrapper1.between("create_time", format + "-01-01 00:00:00", format + "-12-31 23:59:59");
            } else {
                Calendar calendar = Calendar.getInstance();
                if (null == number) {
                    calendar.add(Calendar.MONTH, -13);
                } else {
                    calendar.add(Calendar.MONTH, -number);
                }
                queryWrapper1.between("create_time", calendar.getTime(), new Date());
//                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//                String format = sdf.format(productionYield.getCreateTime());
//                queryWrapper1.between("create_time", format + "-01 00:00:00", format + "-31 23:59:59");
            }
            queryWrapper1.select("convert(IFNULL(sum(production_plan),'0'),decimal(10,2)) productionPlan");
            ProductionWritePlan writePlan = writePlanMapper.selectOne(queryWrapper1);
//            if (ObjectUtil.isEmpty(writePlan) && writePlan.getIsErp() == 1){
//                productionYield.setActualProduction(df.format(Double.parseDouble(writePlan.getActualProduction() )* Double.parseDouble(conver)));
//            }else {
//                productionYield.setActualProduction(df.format(Double.parseDouble(productionYield.getActualProduction() )* Double.parseDouble(conver)));
//            }
            if (ObjectUtil.isEmpty(writePlan) || "0".equals(writePlan.getProductionPlan())) {
                productionYield.setCompleteRate("0");
                productionYield.setPlanProduction("0");
            } else {
                double v = Double.valueOf(productionYield.getActualProduction()) / Double.valueOf(writePlan.getProductionPlan());
                DecimalFormat def = new DecimalFormat("0.00");
                productionYield.setPlanProduction(df.format(Double.parseDouble(writePlan.getProductionPlan())* Double.parseDouble(conver)));
                if (v > 1.0) {
                    productionYield.setCompleteRate("100");
                } else {
                    productionYield.setCompleteRate(def.format(v * 100));
                }
            }
        }
        return productionYields;
    }

}
