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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.yb.production.dao.ProductionCircleRecipeDao;
import com.sw.mes.yb.production.dao.ProductionCircleRecipeDetailDao;
import com.sw.mes.yb.production.dao.ProductionCircleWeightDao;
import com.sw.mes.yb.production.dao.ProductionCircleWeightDetailDao;
import com.sw.mes.yb.production.dto.ProductionCircleRecipeDetailDto;
import com.sw.mes.yb.production.dto.ProductionCircleRecipeDetailVo;
import com.sw.mes.yb.production.entity.ProductionCircleRecipe;
import com.sw.mes.yb.production.entity.ProductionCircleRecipeDetail;
import com.sw.mes.yb.production.entity.ProductionCircleWeight;
import com.sw.mes.yb.production.entity.ProductionCircleWeightDetail;
import com.sw.mes.yb.production.service.ProductionCircleRecipeDetailService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class ProductionCircleRecipeDetailServiceImpl extends BaseServiceImpl<ProductionCircleRecipeDetail> implements ProductionCircleRecipeDetailService {
    ProductionCircleRecipeDetailDao dao;
    ProductionCircleRecipeDao dao1;
    ProductionCircleWeightDao dao2;
    ProductionCircleWeightDetailDao dao3;
    @Autowired
    ModelMapper modelMapper;
    @Autowired
    public void setDao(ProductionCircleRecipeDetailDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    public void setDao(ProductionCircleRecipeDao dao1) {
        super.setBaseDao(dao1);
        this.dao1=dao1;
    }

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

    @Autowired
    public void setDao(ProductionCircleWeightDetailDao dao3) {
        super.setBaseDao(dao3);
        this.dao3=dao3;
    }

    @Override
    public List<ProductionCircleRecipeDetailVo> getRecipeDetailsById(String recipeName ,String recipeCode, Integer version) {
        //获取配方下所有的详情数据
        List<ProductionCircleRecipeDetail> list = dao.getRecipeDetailsById(recipeName,recipeCode,version);
        List<String> list1 = dao.getRecipePurpose(recipeName,recipeCode,version);
        List<ProductionCircleRecipeDetailVo> list2 = new ArrayList<>();
        if (list != null && list1 != null){
            //根据配方用途分组
            Map<String, List<ProductionCircleRecipeDetail>> collect = list.stream().collect(Collectors.groupingBy(s));
            for (int q = 0;q < collect.size(); q++){
                //根据配方种类分组
                Map<String, List<ProductionCircleRecipeDetail>> collect1 = collect.get(list1.get(q)).stream().collect(Collectors.groupingBy(ma));
                ProductionCircleRecipeDetailVo date = new ProductionCircleRecipeDetailVo();
                date.setRecipePurpose(list1.get(q));
                date.setKind(collect1);
                list2.add(date);
            }
            return list2;
        }
        return null;
    }

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

    @Override
    public List<ProductionCircleRecipeDetailVo> getRecipeDetailById(Long id,Long weightId) {
        ProductionCircleRecipe obj = dao1.getRecipeWeightById(id);
        if (obj == null) {
            return null;
        }
        if (weightId != null && weightId > 0){
            ProductionCircleWeight obj1 = dao2.getRecipeWeightById(weightId);
            if (obj1 != null){
                List<ProductionCircleRecipeDetail> list4 = dao.getRecipeDetailsById(obj.getRecipeName(),obj.getRecipeCode(),obj.getVersion());
                List<ProductionCircleRecipeDetailDto> list = list4.stream()
                        .map(ProductionCircleRecipeDetail -> toLazyDto(ProductionCircleRecipeDetail))
                        .collect(Collectors.toList());
                //TODO 实际业务要修改
//                List<ProductionCircleWeightDetail> list1 = dao3.getRecipeWeightDetail(obj1.getWeightName(),obj1.getWeightCode(),obj1.getVersion());
//                if (list.size() > 0){
//                    for (ProductionCircleRecipeDetailDto productionCircleRecipeDetail : list) {
//                        for (ProductionCircleWeightDetail productionCircleWeightDetail : list1) {
//                            if (productionCircleRecipeDetail.getRecipePurpose().equals(productionCircleWeightDetail.getRecipePurpose())
//                                    && productionCircleRecipeDetail.getRecipeKind().equals(productionCircleWeightDetail.getRecipeKind())) {
//                                productionCircleRecipeDetail.setWeight(productionCircleWeightDetail.getWeight());
//                            }
//                        }
//                    }
//                }
                List<String> list3 = dao.getRecipePurpose(obj.getRecipeName(),obj.getRecipeCode(),obj.getVersion());
                List<ProductionCircleRecipeDetailVo> list2 = new ArrayList<>();
                if (list != null && list3 != null){
                    //根据配方用途分组
                    Map<String, List<ProductionCircleRecipeDetailDto>> collect = list.stream().collect(Collectors.groupingBy(ss));
                    for (int q = 0;q < collect.size(); q++){
                        //根据配方种类分组
                        Map<String, List<ProductionCircleRecipeDetailDto>> collect1 = collect.get(list3.get(q)).stream().collect(Collectors.groupingBy(m));
                        ProductionCircleRecipeDetailVo date = new ProductionCircleRecipeDetailVo();
                        date.setRecipePurpose(list3.get(q));
                        date.setKind(collect1);
                        list2.add(date);
                    }
                    return list2;
                }
                return null;
            }
        }
        return getRecipeDetailsById(obj.getRecipeName(),obj.getRecipeCode(),obj.getVersion());
    }

    @Override
    public List<ProductionCircleRecipeDetail> getProductionCircleRecipeDetailsByParam(Long recipeId, String againNum) {
        return dao.getProductionCircleRecipeDetailsByParam(recipeId,againNum);
    }

    @Override
    public List<String> getRecipeMaterialTypeNamesByParam(Long recipeId, String againNum) {
        return dao.getRecipeMaterialTypeNamesByParam(recipeId,againNum);
    }

    @Override
    public List<ProductionCircleRecipeDetail> getProductionCircleRecipeDetailsByParamAccurate(Long recipeId, String againNum) {
        return dao.getProductionCircleRecipeDetailsByParamAccurate(recipeId,againNum);
    }

    @Override
    public List<String> getRecipeMaterialTypeNamesByParamAccurate(Long recipeId, String againNum) {
        return dao.getRecipeMaterialTypeNamesByParamAccurate(recipeId, againNum);
    }

    @Override
    public List<ProductionCircleRecipeDetail> getProductionRecipeDetailsByParam(Long recipeId, String againNum) {
        return dao.getProductionRecipeDetailsByParam(recipeId, againNum);
    }

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


    public ProductionCircleRecipeDetailDto toLazyDto(ProductionCircleRecipeDetail entity) {
        return modelMapper.map(entity, ProductionCircleRecipeDetailDto.class);
    }

    Function<ProductionCircleRecipeDetail, String> s = new Function<ProductionCircleRecipeDetail, String>() {

        @Override
        public String apply(ProductionCircleRecipeDetail t) {
            Object object = t.getRecipePurpose();
            String string = object.toString();
            return string;
        }
    };

    Function<ProductionCircleRecipeDetailDto, String>  ss = new Function<ProductionCircleRecipeDetailDto, String>() {

        @Override
        public String apply(ProductionCircleRecipeDetailDto t) {
            Object object = t.getRecipePurpose();
            String string = object.toString();
            return string;
        }
    };

    Function<ProductionCircleRecipeDetail, String>  ma = new Function<ProductionCircleRecipeDetail, String>() {

        @Override
        public String apply(ProductionCircleRecipeDetail t) {
            Object object = t.getRecipeKind();
            if(object == null){
                return "";
            }
            String string = object.toString();
            return string;
        }
    };
    Function<ProductionCircleRecipeDetailDto, String>  m = new Function<ProductionCircleRecipeDetailDto, String>() {

        @Override
        public String apply(ProductionCircleRecipeDetailDto t) {
            Object object = t.getRecipeKind();
            String string = object.toString();
            return string;
        }
    };
}
