package com.aucma.production.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.aucma.base.domain.BaseOrderInfo;
import com.aucma.base.service.IBaseOrderInfoService;
import com.aucma.base.utils.MaterialConstants;
import com.aucma.common.exception.ServiceException;
import com.aucma.common.exception.base.BaseException;
import com.aucma.common.utils.DateUtils;
import com.aucma.common.utils.StringUtils;
import com.aucma.common.utils.uuid.PlanCodeUtils;
import com.aucma.production.domain.BaseBomInfo;
import com.aucma.production.service.IBaseBomInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.aucma.production.mapper.ProductPlanInfoMapper;
import com.aucma.production.domain.ProductPlanInfo;
import com.aucma.production.service.IProductPlanInfoService;

import static com.aucma.common.utils.SecurityUtils.getUsername;

/**
 * 生产计划Service业务层处理
 *
 * @author Yinq
 * @date 2023-10-07
 */
@Service
public class ProductPlanInfoServiceImpl implements IProductPlanInfoService {
    @Autowired
    private ProductPlanInfoMapper productPlanInfoMapper;

    @Autowired
    private IBaseOrderInfoService baseOrderInfoService;

    @Autowired
    private IBaseBomInfoService baseBomInfoService;

    /**
     * 查询生产计划
     *
     * @param objId 生产计划主键
     * @return 生产计划
     */
    @Override
    public ProductPlanInfo selectProductPlanInfoByObjId(Long objId) {
        return productPlanInfoMapper.selectProductPlanInfoByObjId(objId);
    }

    /**
     * 查询生产计划列表
     *
     * @param productPlanInfo 生产计划
     * @return 生产计划
     */
    @Override
    public List<ProductPlanInfo> selectProductPlanInfoList(ProductPlanInfo productPlanInfo) {
        return productPlanInfoMapper.selectProductPlanInfoList(productPlanInfo);
    }

    /**
     * 新增生产计划
     *
     * @param productPlanInfo 生产计划
     * @return 结果
     */
    @Override
    public int insertProductPlanInfo(ProductPlanInfo productPlanInfo) {
        productPlanInfo.setCreatedTime(DateUtils.getNowDate());
        try {
            ProductPlanInfo info = new ProductPlanInfo();
            info.setOrderCode(productPlanInfo.getOrderCode());
            info.setProductLineCode(productPlanInfo.getProductLineCode());
            List<ProductPlanInfo> productPlanInfos = this.selectProductPlanInfoList(info);
            if (productPlanInfos.size() > 0){
                productPlanInfo.setUpdatedBy("user");
                productPlanInfo.setObjId(productPlanInfos.get(0).getObjId());
                return productPlanInfoMapper.updateProductPlanInfo(productPlanInfo);
            }else {
                return productPlanInfoMapper.insertProductPlanInfo(productPlanInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 修改生产计划
     *
     * @param productPlanInfo 生产计划
     * @return 结果
     */
    @Override
    public int updateProductPlanInfo(ProductPlanInfo productPlanInfo) {
        productPlanInfo.setUpdatedTime(DateUtils.getNowDate());
        return productPlanInfoMapper.updateProductPlanInfo(productPlanInfo);
    }

    /**
     * 批量删除生产计划
     *
     * @param objIds 需要删除的生产计划主键
     * @return 结果
     */
    @Override
    public int deleteProductPlanInfoByObjIds(Long[] objIds) {
        return productPlanInfoMapper.deleteProductPlanInfoByObjIds(objIds);
    }

    /**
     * 删除生产计划信息
     *
     * @param objId 生产计划主键
     * @return 结果
     */
    @Override
    public int deleteProductPlanInfoByObjId(Long objId) {
        return productPlanInfoMapper.deleteProductPlanInfoByObjId(objId);
    }

    /**
     * 工单计划下达
     *
     * @param baseOrderInfo
     * @return
     */
    @Override
    public Boolean releaseOrderPlan(BaseOrderInfo baseOrderInfo) {
        String orderCode = baseOrderInfo.getOrderCode();
        String saleOrderCode = baseOrderInfo.getSaleOrderCode();
        String saleOrderLineNumber = baseOrderInfo.getSaleOrderLineNumber();
        BigDecimal orderAmount = baseOrderInfo.getOrderAmount();
        if (orderAmount.compareTo(new BigDecimal(1)) < 0) {
            throw new BaseException("该工单计划数量不合法！");
        }
        //获取生产BOM信息
        BaseBomInfo bomInfo = new BaseBomInfo();
        bomInfo.setMaterialCode(baseOrderInfo.getMaterialCode());
        bomInfo.setIsPlanToStation(0L);
        List<BaseBomInfo> baseBomInfoList = baseBomInfoService.selectBaseBomInfoList(bomInfo);
        //检查生产BOM是否满足下计划条件
        if (baseBomInfoList.size() == 0) {
            throw new ServiceException("生产BOM信息不完整，请先维护生产BOM信息！");
        }
        BaseBomInfo bom = new BaseBomInfo();
        bom.setAncestors(baseOrderInfo.getMaterialCode());
        List<BaseBomInfo> baseBomInfos = baseBomInfoService.selectBaseBomInfoList(bom);
        try {
            //一线
            if (StringUtils.isNotEmpty(baseOrderInfo.getWorkCenterCode()) && baseOrderInfo.getWorkCenterCode().equals("3101")) {
                for (BaseBomInfo baseBomInfo : baseBomInfos) {
                    if (baseBomInfo.getMaterialType().equals(MaterialConstants.BOX_MATERIAL_TYPE)) {
                        ProductPlanInfo planInfo = new ProductPlanInfo();
                        planInfo.setPlanCode(PlanCodeUtils.getPlanCode());
                        planInfo.setOrderCode(orderCode);
                        planInfo.setSaleOrderCode(saleOrderCode);
                        planInfo.setSaleorderLinenumber(saleOrderLineNumber);
                        planInfo.setMaterialCode(baseBomInfo.getMaterialCode());
                        planInfo.setMaterialName(baseBomInfo.getMaterialName());
                        planInfo.setProductLineCode("1105");//箱体发泡
                        planInfo.setPlanAmount(baseBomInfo.getStandardAmount().longValue() * orderAmount.longValue());
                        this.insertProductPlanInfo(planInfo);
                        planInfo.setPlanCode(PlanCodeUtils.getPlanCode());
                        planInfo.setProductLineCode("1109");//成品下线扫描
                        this.insertProductPlanInfo(planInfo);
                    }
                }
            }
            //二线
            if (StringUtils.isNotEmpty(baseOrderInfo.getWorkCenterCode()) && baseOrderInfo.getWorkCenterCode().equals("3103")) {
                for (BaseBomInfo baseBomInfo : baseBomInfos) {
                    if (baseBomInfo.getMaterialType().equals(MaterialConstants.COAMING_MATERIAL_TYPE)) {
                        ProductPlanInfo plan = new ProductPlanInfo();
                        plan.setPlanCode(PlanCodeUtils.getPlanCode());
                        plan.setProductLineCode("1001");// 前/后板自动成型
                        plan.setOrderCode(orderCode);
                        plan.setSaleOrderCode(saleOrderCode);
                        plan.setSaleorderLinenumber(saleOrderLineNumber);
                        plan.setMaterialCode(baseBomInfo.getMaterialCode());
                        plan.setMaterialName(baseBomInfo.getMaterialName());
                        plan.setPlanAmount(baseBomInfo.getStandardAmount().longValue() * orderAmount.longValue());
                        this.insertProductPlanInfo(plan);
                    }
                    if (baseBomInfo.getMaterialType().equals(MaterialConstants.BOX_MATERIAL_TYPE)) {
                        ProductPlanInfo planInfo = new ProductPlanInfo();
                        planInfo.setPlanCode(PlanCodeUtils.getPlanCode());
                        planInfo.setOrderCode(orderCode);
                        planInfo.setSaleOrderCode(saleOrderCode);
                        planInfo.setSaleorderLinenumber(saleOrderLineNumber);
                        planInfo.setMaterialCode(baseBomInfo.getMaterialCode());
                        planInfo.setMaterialName(baseBomInfo.getMaterialName());
                        planInfo.setPlanAmount(baseBomInfo.getStandardAmount().longValue() * orderAmount.longValue());
                        planInfo.setProductLineCode("1002");// 胆、壳组装
                        this.insertProductPlanInfo(planInfo);
                        planInfo.setPlanCode(PlanCodeUtils.getPlanCode());
                        planInfo.setProductLineCode("1005");// 箱体发泡
                        this.insertProductPlanInfo(planInfo);
                        planInfo.setPlanCode(PlanCodeUtils.getPlanCode());
                        planInfo.setProductLineCode("1006");// 泡后库
                        this.insertProductPlanInfo(planInfo);
                    }
                }
            }
            baseOrderInfo.setIsRelease(0L);
            baseOrderInfoService.updateBaseOrderInfo(baseOrderInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
        return true;
    }

}
