package	com.sw.mes.yb.production.service.impl;


import com.sw.mes.core.enums.EnumValuesFactory;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.base.workinprocess.entity.PlanProcessingOrder;
import com.sw.mes.base.workinprocess.service.PlanProcessingOrderService;
import com.sw.mes.yb.production.dao.ProductionRecycledMaterialDao;
import com.sw.mes.yb.production.entity.ProductionRecycledMaterial;
import com.sw.mes.yb.production.service.ProductionRecycledMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class ProductionRecycledMaterialServiceImpl extends BaseServiceImpl<ProductionRecycledMaterial> implements ProductionRecycledMaterialService {
    ProductionRecycledMaterialDao dao;
	@Autowired
    public void setDao(ProductionRecycledMaterialDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private PlanProcessingOrderService planProcessingOrderService;

    @Override
    public Result saveOrUpdateInfo(ProductionRecycledMaterial productionRecycledMaterial) {
        String equipmentCode = "";
        try{
            //根据炉次号查询炉台号
            PlanProcessingOrder planProcessingOrder = planProcessingOrderService.findByCodeAndOrderTypeAndStatus(productionRecycledMaterial.getLotNo(), EnumValuesFactory.PlanProcessingOrderOrderType.炉次.getKey(), EnumValuesFactory.PlanProcessingOrderStatus.加工中.getKey());
            if (planProcessingOrder != null) {
                equipmentCode = planProcessingOrder.getEquipmentCode();
            } else {
                equipmentCode = productionRecycledMaterial.getLotNo() != null && productionRecycledMaterial.getLotNo().length() > 7 ? productionRecycledMaterial.getLotNo().substring(4,7) : "";
            }
        }catch (Exception e){

        }
        productionRecycledMaterial.setDeviceNo(equipmentCode);
	    if(productionRecycledMaterial.getId() == null){//新增
            productionRecycledMaterial.setCreateBy(BaseContextHandler.getUsername());
            productionRecycledMaterial.setCreateTime(new Date());
        }else{
            productionRecycledMaterial.setUpdateUser(BaseContextHandler.getUsername());
            productionRecycledMaterial.setUpdateTime(new Date());
        }
        this.saveOrUpdate(productionRecycledMaterial);
        return ResultGenerator.success();
    }
}
