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.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)
    {
        if (StringUtils.isEmpty(productPlanInfo.getCreatedBy())){
            productPlanInfo.setCreatedBy(getUsername());
        }
        productPlanInfo.setCreatedTime(DateUtils.getNowDate());
        return productPlanInfoMapper.insertProductPlanInfo(productPlanInfo);
    }

    /**
     * 修改生产计划
     *
     * @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) {

        try {
            String orderCode = baseOrderInfo.getOrderCode();
            String saleOrderCode = baseOrderInfo.getSaleOrderCode();
            String saleOrderLineNumber = baseOrderInfo.getSaleOrderLineNumber();
            BigDecimal orderAmount = baseOrderInfo.getOrderAmount();
            if (StringUtils.isNull(orderAmount) || (orderAmount.compareTo(new BigDecimal(1))==-1)){
                throw new BaseException("该工单计划数量不合法！");
            }
            //获取生产BOM信息
            BaseBomInfo bomInfo = new BaseBomInfo();
            bomInfo.setParentId(baseOrderInfo.getMaterialCode());
            List<BaseBomInfo> baseBomInfoList = baseBomInfoService.selectBaseBomInfoList(bomInfo);
            if (baseBomInfoList.size() == 0){
                throw new BaseException("请先维护生产BOM信息！");
            }
            //-- 若一个工单可下达多计划：则需要根据工单编号和物料编号去重

            //检查生产BOM是否满足下计划条件
            baseBomInfoService.checkBomInfo(baseBomInfoList);
            //维护生产计划
            for (BaseBomInfo baseBomInfo : baseBomInfoList) {
                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(baseBomInfo.getProductLineCode());
                planInfo.setPlanAmount(baseBomInfo.getStandardAmount() * orderAmount.longValue());
                this.insertProductPlanInfo(planInfo);
            }
            baseOrderInfo.setIsRelease(0L);
            baseOrderInfoService.updateBaseOrderInfo(baseOrderInfo);
        } catch (Exception e) {
            throw new BaseException(e.getMessage());
        }
        return true;
    }

}
