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.fjhx.item.service.product.ProductInfoService;
import com.fjhx.mes.entity.work.po.WorkOrderBom;
import com.fjhx.mes.mapper.work.WorkOrderBomMapper;
import com.fjhx.mes.service.work.WorkOrderBomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.mes.entity.bom.po.BomDetail;
import com.fjhx.mes.entity.bom.po.BomInfo;
import com.fjhx.mes.entity.work.po.WorkOrder;
import com.fjhx.mes.service.bom.BomDetailService;
import com.fjhx.mes.service.bom.BomInfoService;
import com.fjhx.mes.service.work.WorkOrderService;
import com.obs.services.internal.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fjhx.mes.entity.work.vo.WorkOrderBomVo;
import com.fjhx.mes.entity.work.dto.WorkOrderBomDto;

import java.util.List;

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

    @Autowired
    private BomInfoService bomInfoService;
    @Autowired
    private BomDetailService bomDetailService;
    @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()));
        if(ObjectUtil.isEmpty(workOrderBomList)){
            //如果不存在定制BOM就查询基本BOM信息
            BomInfo bomInfo = bomInfoService.getOne(q -> q.eq(BomInfo::getProductId, workOrder.getProductId()).eq(BomInfo::getCurrentVersion,1));
            if(ObjectUtil.isEmpty(bomInfo)){
                throw new ServiceException("查询不到产品基本BOM信息");
            }
            List<BomDetail> bomDetailList = bomDetailService.list(q -> q.eq(BomDetail::getBomInfoId, bomInfo.getId()));
            if(ObjectUtil.isEmpty(bomDetailList)){
                throw new ServiceException("查询不到产品基本BOM明细信息");
            }
            workOrderBomList = BeanUtil.copyToList(bomDetailList, WorkOrderBom.class);
            for (WorkOrderBom workOrderBomVo : workOrderBomList) {
                workOrderBomVo.setId(null);//清空id
            }
        }
        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());
        });
        return workOrderBomVos;
    }

    @DSTransactional
    @Override
    public void edit(WorkOrderBomDto workOrderBomDto) {
//        this.updateById(workOrderBomDto);

        List<WorkOrderBom> afterSaleDetailList = workOrderBomDto.getWorkOrderBomList();
        afterSaleDetailList.forEach(item -> item.setWorkOrderId(workOrderBomDto.getWorkOrderId()));
        this.editLinked(afterSaleDetailList, WorkOrderBom::getWorkOrderId, workOrderBomDto.getWorkOrderId());

    }

}
