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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.bom.vo.BomDetailVo;
import com.fjhx.mes.entity.bom.vo.BomInfoVo;
import com.fjhx.mes.entity.work.dto.WorkOrderBomDto;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.entity.work.po.WorkOrderBom;
import com.fjhx.mes.entity.work.vo.WorkOrderBomVo;
import com.fjhx.mes.mapper.work.WorkOrderBomMapper;
import com.fjhx.mes.service.bom.BomInfoService;
import com.fjhx.mes.service.work.WorkOrderBomService;
import com.fjhx.mes.service.work.WorkOrderService;
import com.ruoyi.common.core.domain.BaseIdPo;
import com.ruoyi.common.core.domain.BasePo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单bom 服务实现类
 * </p>
 *
 * @author
 * @since 2023-05-17
 */
@Service
public class WorkOrderBomServiceImpl extends ServiceImpl<WorkOrderBomMapper, WorkOrderBom> implements WorkOrderBomService {

    @Autowired
    private BomInfoService bomInfoService;
    @Autowired
    private WorkOrderService workOrderService;
    @Autowired
    private ProductInfoService productInfoService;

    @Override
    public List<WorkOrderBomVo> getList(WorkOrderBom dto) {
        WorkOrder workOrder = workOrderService.getById(dto.getWorkOrderId());
        if (ObjectUtil.isEmpty(workOrder)) {
            throw new ServiceException("查询不到工单信息");
        }
        List<WorkOrderBom> workOrderBomList = list(q -> q.eq(WorkOrderBom::getWorkOrderId, dto.getWorkOrderId()));

        //如果工单BOM为空就查询基本BOM信息
//        if (ObjectUtil.isEmpty(workOrderBomList)) {
//            BomInfoVo bomInfo = bomInfoService.getProductBom(workOrder.getProductId());
//            if (ObjectUtil.isNotEmpty(bomInfo)) {
//                List<BomDetailVo> bomDetailList = bomInfo.getBomDetailVoList();
//                if (ObjectUtil.isNotEmpty(bomDetailList)) {
//                    bomDetailList.forEach(item -> item.setId(null));//清空复制过来的id
//                    workOrderBomList = BeanUtil.copyToList(bomDetailList, WorkOrderBom.class);
//                }
//            }
//        }

        List<WorkOrderBomVo> workOrderBomVos = BeanUtil.copyToList(workOrderBomList, WorkOrderBomVo.class);
        //赋值产品明细
        productInfoService.attributeAssign(workOrderBomVos, WorkOrderBomVo::getProductId, (item, productInfo) -> {
            item.setProductCode(productInfo.getCode());
            item.setProductUnit(productInfo.getUnit());
            item.setProductName(productInfo.getName());
            item.setProductSpec(productInfo.getSpec());
        });
        return workOrderBomVos;
    }

    @DSTransactional
    @Override
    public void edit(WorkOrderBomDto workOrderBomDto) {
        workOrderService.update(q -> q
                .eq(WorkOrder::getId, workOrderBomDto.getWorkOrderId())
                .set(ObjectUtil.isNotEmpty(workOrderBomDto.getBomStatus()), WorkOrder::getBomStatus, workOrderBomDto.getBomStatus())
                .set(BasePo::getUpdateTime, new Date())
                .set(BasePo::getUpdateUser, SecurityUtils.getUserId())
        );

        List<WorkOrderBom> workOrderBomList = workOrderBomDto.getWorkOrderBomList();
        workOrderBomList.forEach(item -> item.setWorkOrderId(workOrderBomDto.getWorkOrderId()));

        //处理被删除的数据
        List<Long> workOrderBomIds = workOrderBomList.stream().map(BaseIdPo::getId).filter(Objects::nonNull).collect(Collectors.toList());
        this.remove(q -> q
                .eq(ObjectUtil.isNotEmpty(workOrderBomDto.getType()),WorkOrderBom::getType, workOrderBomDto.getType())
                .eq(WorkOrderBom::getWorkOrderId, workOrderBomDto.getWorkOrderId())
                .notIn(ObjectUtil.isNotEmpty(workOrderBomIds), BaseIdPo::getId, workOrderBomIds));
        this.saveOrUpdateBatch(workOrderBomList);

        //检查定制工单是否配置BOM、工艺 配置完成发消息
        workOrderService.checkWorkOrderStatus(workOrderBomDto.getWorkOrderId());
    }

}
