/**
* 文件：MateriaBomItemServiceImpl.java
* 版本：1.0.0
* 日期：
* Copyright &reg; 
* All right reserved.
*/

package com.mortals.xhx.module.materia.service.impl;

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDCacheServiceImpl;
import com.mortals.xhx.common.code.BomStatusEnum;
import com.mortals.xhx.module.materia.dao.MateriaBomItemDao;
import com.mortals.xhx.module.materia.model.*;
import com.mortals.xhx.module.materia.service.MateriaBomItemService;
import com.mortals.xhx.module.materia.service.MateriaBomService;
import com.mortals.xhx.module.materia.service.MateriaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>Title: 物料BOM单项表</p>
 * <p>Description: MateriaBomItemServiceImpl service接口 </p>
 * <p>Copyright: Copyright &reg;  </p>
 * <p>Company: </p>
 * @author 
 * @version 1.0.0
 */
@Service("materiaBomItemService")
public class MateriaBomItemServiceImpl extends AbstractCRUDCacheServiceImpl<MateriaBomItemDao, MateriaBomItemEntity,Long> implements MateriaBomItemService {

    @Autowired
    private MateriaService materiaService;
    @Autowired
    private MateriaBomService materiaBomService;

    @Override
    protected void saveBefore(MateriaBomItemEntity entity, Context context) throws AppException {

        if (ObjectUtils.isEmpty(entity.getMateriaCode())) {
            throw new AppException("当前物料编码不存在，code:"+entity.getMateriaCode());
        }
        MateriaQuery materiaQuery = new MateriaQuery();
        materiaQuery.setCode(entity.getMateriaCode());
        MateriaEntity materiaEntity = materiaService.find(materiaQuery, context).stream().findFirst().orElseGet(()->null);

        //禁止添子bom时候新增物料
        if (ObjectUtils.isEmpty(materiaEntity)) {
            throw new AppException("当前物料编码不存在，code:"+entity.getMateriaCode());
        }
        //如果存在，则使用现有物料编码
        entity.setMateriaId(materiaEntity.getId());
        entity.setMateriaCode(materiaEntity.getCode());
        entity.setMateriaName(materiaEntity.getName());

        MateriaBomQuery bomQuery = new MateriaBomQuery();
        bomQuery.setMateriaId(materiaEntity.getId());
        MateriaBomEntity bomEntity = materiaBomService.find(bomQuery, context).stream().findAny().orElseGet(() -> null);
        if(!ObjectUtils.isEmpty(bomEntity)){
            if(bomEntity.getId().equals(entity.getBomId())){
                throw new AppException("bom子项不能添加父bom项!");
            }
           // entity.setMateriaBomId(bomEntity.getId());
        }

        //查询物料子项是否已经存在，如果存在也不导入
        MateriaBomItemQuery materiaBomItemQuery = new MateriaBomItemQuery();
        materiaBomItemQuery.setMateriaId(entity.getMateriaId());
        materiaBomItemQuery.setBomId(entity.getBomId());

        int count = this.count(materiaBomItemQuery, context);
        if(count>0){
            throw new AppException("已存在相同物料项！");
        }


        MateriaBomEntity materiaBomEntity = materiaBomService.get(entity.getBomId(), context);
        if(materiaBomEntity.getStatus()!= BomStatusEnum.BOM_EDIT.getValue()){
            throw new AppException("BOM物料清单当前状态不能修改！");
        }

    }

    @Override
    protected void updateBefore(MateriaBomItemEntity entity, Context context) throws AppException {

        MateriaBomEntity bomEntity = materiaBomService.get(entity.getBomId(), context);
        if (bomEntity.getStatus() != BomStatusEnum.BOM_EDIT.getValue()) {
            throw new AppException("只有修订中的Bom清单才能修改");
        }


        super.updateBefore(entity, context);
    }

    @Override
    public void updateItem(MateriaBomItemEntity entity) {
        this.getDao().update(entity);
    }
}