package vip.xiaonuo.production.service.impl;

import cn.hutool.core.date.DateUtil;
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.ProduceLocation;
import vip.xiaonuo.production.entity.ProductionWorkstopPlan;
import vip.xiaonuo.production.entity.vo.ProductionPlanVo;
import vip.xiaonuo.production.entity.vo.ProductionWorkPlanVo;
import vip.xiaonuo.production.mapper.ProduceLocationMapper;
import vip.xiaonuo.production.mapper.ProductionUnitCostMapper;
import vip.xiaonuo.production.mapper.ProductionWorkstopPlanMapper;
import vip.xiaonuo.production.param.ProductionPlanParam;
import vip.xiaonuo.production.service.IProductionWorkstopPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 生成管控车间计划 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-12-12
 */
@Service
public class ProductionWorkstopPlanServiceImpl extends ServiceImpl<ProductionWorkstopPlanMapper, ProductionWorkstopPlan> implements IProductionWorkstopPlanService {

    @Autowired
    private ProductionWorkstopPlanMapper planMapper;

    @Autowired
    private ProductionUnitCostMapper unitCostMapper;

    @Autowired
    private ProduceLocationMapper locationMapper;

    @Override
    public List<ProductionPlanVo> getList(ProductionPlanParam planParam) {
        String orgId = planParam.getOrgId();
        String produceName = planParam.getProduceName();
        QueryWrapper<ProduceLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        List<ProduceLocation> locations = locationMapper.selectList(queryWrapper);
        List<ProductionPlanVo> list = new ArrayList<>();
        if (locations.size() != 0) {
            for (ProduceLocation location : locations) {
                ProductionPlanVo planVo = new ProductionPlanVo();
                planVo.setLocationName(location.getLocationName());

                QueryWrapper<ProductionWorkstopPlan> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.ge("create_time", DateUtil.beginOfYear(new Date()));
                queryWrapper1.eq("location_name", location.getId());
                queryWrapper1.eq("produce_name", produceName);
                queryWrapper1.select(" IFNULL(sum(plan_production),'0') planProduction,IFNULL(sum(actual_production),'0') actualProduction," +
                        "produce_name produceName");
                ProductionWorkstopPlan workstopPlan = planMapper.selectOne(queryWrapper1);
                planVo.setProduceName(workstopPlan.getProduceName());
                planVo.setActualProduction(workstopPlan.getActualProduction());
                planVo.setPlanProduction(workstopPlan.getPlanProduction());

                QueryWrapper<ProductionWorkstopPlan> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.ge("create_time", DateUtil.beginOfYear(new Date()));
                queryWrapper2.eq("location_name", location.getId());
                queryWrapper2.eq("produce_name", produceName);
                queryWrapper2.select(" convert(IFNULL(avg(unit_cost),'0'),decimal(10,2)) unitCost,convert(IFNULL(avg(avg_value),'0'),decimal(10,2)) avgValue," +
                        "create_time createTime,produce_name produceName");
                queryWrapper2.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
                List<ProductionWorkstopPlan> plans = planMapper.selectList(queryWrapper2);
                List<ProductionWorkPlanVo> workPlanVos = new ArrayList<>();
                if (plans.size() != 0) {
                    for (ProductionWorkstopPlan plan : plans) {
                        ProductionWorkPlanVo vo = new ProductionWorkPlanVo();
                        vo.setAvgValue(plan.getAvgValue());
                        vo.setProduceName(plan.getProduceName());
                        vo.setCreateTime(plan.getCreateTime());
                        vo.setUnitCost(plan.getUnitCost());
                        workPlanVos.add(vo);
                    }
                }

                planVo.setWorkPlanVo(workPlanVos);
                list.add(planVo);
            }
        }

        return list;
    }
}
