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


import java.util.List;
import java.util.Optional;

import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.utils.RequestPage;
import com.sw.mes.base.factory.utils.PageUtils;
import com.sw.mes.yb.production.dao.ProductionCircleRecipeDao;
import com.sw.mes.yb.production.entity.ProductionCircleRecipe;
import com.sw.mes.yb.production.service.ProductionCircleRecipeService;


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

    @Override
    public List<ProductionCircleRecipe> findByExample(Specification<ProductionCircleRecipe> specification, RequestPage page) {
        org.springframework.data.domain.Page<ProductionCircleRecipe> springDataPage = dao.findAll(specification, PageUtils.createPageable(page));
        page.setTotal(springDataPage.getTotalElements());
        return springDataPage.getContent();
    }

    @Override
    public Result updateStatus(Long id, Integer status) {
        if (status == 0) {
            // 查询当前配方和编码是否存在启用,存在及不可再启用
            Optional<ProductionCircleRecipe> byId = dao.findById(id);
            ProductionCircleRecipe recipe = byId.orElse(null);
            if (null == recipe) {
                return ResultGenerator.fail("配方ID【" + id + "】不存在，请刷新页面重试");
            }
            String recipeName = recipe.getRecipeName();
            String recipeCode = recipe.getRecipeCode();
            Integer openCount = dao.getRecipeByCodeOrName(recipeName, recipeCode);
            if (openCount > 0) {
                return ResultGenerator.fail("已存在该配料名称或编码已启用的配方");
            }
            dao.updateStatus(id, 1);
        } else {
            dao.updateStatus(id, 0);
        }
        return ResultGenerator.success();
    }


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

    @Override
    public List<String> recipeNameListAll() {
        return dao.recipeNameListAll();
    }

    @Override
    public void deleteAll(String recipeName, String recipeCode, Integer version) {
        dao.deleteAll(recipeName,recipeCode,version);
    }

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

    @Override
    public ProductionCircleRecipe getRecipeByProductCodeOrRecipeName(String recipeName, String productCode) {
        return dao.getRecipeByProductCodeOrRecipeName(recipeName,productCode);
    }

    @Override
    public Integer getNewestVersion(String recipeName, String recipeCode) {
        return dao.getNewestVersion(recipeName,recipeCode);
    }
}
