package com.fjhx.mes.service.production.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.production.bo.WorkshopReportBo;
import com.fjhx.mes.entity.production.dto.ProductionTaskProgressSelectDto;
import com.fjhx.mes.entity.production.po.ProductionTaskProgress;
import com.fjhx.mes.entity.production.vo.ProductionTaskProgressVo;
import com.fjhx.mes.mapper.production.ProductionTaskProgressMapper;
import com.fjhx.mes.service.production.ProductionTaskProgressService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 生产任务进度 服务实现类
 * </p>
 *
 * @author
 * @since 2024-01-15
 */
@Service
public class ProductionTaskProgressServiceImpl extends ServiceImpl<ProductionTaskProgressMapper, ProductionTaskProgress> implements ProductionTaskProgressService {

    @Autowired
    private ProductInfoService productInfoService;

    private IWrapper<ProductionTaskProgress> getWorkshopReportWrapper(ProductionTaskProgressSelectDto dto) {
        IWrapper<ProductionTaskProgress> wrapper = getWrapper();
        wrapper.in("pod.company_id", SecurityUtils.getCompanyIds());
        return wrapper;
    }

    @Override
    public List<ProductionTaskProgressVo> workshopReportStatistics(ProductionTaskProgressSelectDto dto) {
        IWrapper<ProductionTaskProgress> wrapper = getWorkshopReportWrapper(dto);
        wrapper.eq("DATEDIFF(  ps.scheduling_date,now() )", 0);
        wrapper.groupBy("ptp.processes_id");
        List<ProductionTaskProgressVo> workshopReportStatistics = baseMapper.workshopReportStatistics(wrapper);
        return workshopReportStatistics;
    }

    @Override
    public Page<WorkshopReportBo> workshopReport(ProductionTaskProgressSelectDto dto) {
        IWrapper<ProductionTaskProgress> wrapper = getWorkshopReportWrapper(dto);

        //过滤 昨天 今天 明天
        wrapper.in("DATEDIFF(ps.scheduling_date, now())", -1, 0, 1);
        wrapper.groupBy("ptp.id");

        Page<WorkshopReportBo> workshopReportBoPage = baseMapper.workshopReport(dto.getPage(), wrapper);
        List<WorkshopReportBo> records = workshopReportBoPage.getRecords();
        if (ObjectUtil.isEmpty(records)) {
            return workshopReportBoPage;
        }
        //赋值产品信息
        productInfoService.attributeAssign(records, WorkshopReportBo::getProductId, (item, productInfo) -> {
            item.setProductName(productInfo.getName());
            item.setProductCode(productInfo.getCustomCode());
            item.setProductLength(productInfo.getLength());
            item.setProductWidth(productInfo.getWidth());
            item.setProductHeight(productInfo.getHeight());
            item.setProductColor(productInfo.getColor());
        });
        return workshopReportBoPage;
    }
}
