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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.yb.production.dao.ProductionCircleDosingListDao;
import com.sw.mes.yb.production.dao.ProductionCircleWeightDao;
import com.sw.mes.yb.production.entity.ProductionCircleRecipe;
import com.sw.mes.yb.production.entity.ProductionCircleWeight;
import com.sw.mes.yb.production.service.ProductionCircleWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class ProductionCircleWeightServiceImpl extends BaseServiceImpl<ProductionCircleWeight> implements ProductionCircleWeightService {

    ProductionCircleWeightDao dao;

    @Autowired
    public void setDao(ProductionCircleWeightDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        if (status == 0){
            ProductionCircleWeight productionCircleWeight = get(id);
            if(null == productionCircleWeight){
                throw new RuntimeException("该配方不存在");
            }
            Integer count = dao.getOpenCountByCodeAndName(productionCircleWeight.getWeightCode(),productionCircleWeight.getWeightName());
            if(count>0){
                throw new RuntimeException("存在已启用的同种配方");
            }
            dao.updateStatus(id,1);
        }else {
            dao.updateStatus(id,0);
        }
    }

	@Override
	public ProductionCircleWeight getByProductCode(String productCode) {
		// TODO 马国栋 请完成此业务逻辑
		return dao.getByProductCode(productCode);
	}

	@Override
    public void deleteDel(Long id) {
    	dao.deleteDel(id,1);
    }

    @Override
    public Integer getNewestVersion(String weightName, String weightCode) {
        return dao.getNewestVersion(weightName,weightCode);
    }

}
