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


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.yb.production.dao.*;
import com.sw.mes.yb.production.dto.ProductionCircleWeightDto;
import com.sw.mes.yb.production.dto.ProductionCircleWeightDtoVo;
import com.sw.mes.yb.production.entity.*;
import com.sw.mes.yb.production.service.ProductionCircleWeightDetailService;
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 ProductionCircleWeightDetailServiceImpl extends BaseServiceImpl<ProductionCircleWeightDetail> implements ProductionCircleWeightDetailService {
    ProductionCircleWeightDetailDao dao;
    ProductionCircleWeightDao dao1;
    @Autowired
    public void setDao(ProductionCircleWeightDetailDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

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

    @Autowired
    private ProductionCircleDosingListDao productionCircleDosingListDao;

    @Autowired
    private ProductionCircleRecipeDao productionCircleRecipeDao;

    @Autowired
    private ProductionCircleRecipeDetailDao productionCircleRecipeDetailDao;


    @Override
    public void updateStatus(Integer id, Integer status) {

    }

    @Override
    public List<ProductionCircleWeightDtoVo> getRecipeWeightDetail(String weightName,String weightCode, Integer version) {
        //获取配方下所有的详情数据
        List<ProductionCircleWeightDetail> list = dao.getRecipeWeightDetail(weightName,weightCode,version);
        List<String> list1 = dao.getRecipeWeight(weightName,weightCode,version);
        List<ProductionCircleWeightDtoVo> list2 = new ArrayList<>();
        if (list != null && list1 != null){
            //根据配方用途分组
            Map<String, List<ProductionCircleWeightDetail>> collect = list.stream().collect(Collectors.groupingBy(s));
            for (int q = 0;q < collect.size(); q++){
                List<ProductionCircleWeightDetail> collect1 = collect.get(list1.get(q));
                ProductionCircleWeightDtoVo data = new ProductionCircleWeightDtoVo();
                data.setRecipePurpose(list1.get(q));
                data.setKind(collect1);
                list2.add(data);
            }
            return list2;
        }
        return null;
    }


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

    @Override
    public List<ProductionCircleWeightDtoVo> getRecipeWeightDetailById(Long id) {
        ProductionCircleWeight obj = dao1.getRecipeWeightById(id);
        if (obj == null) {
            return null;
        }
        return getRecipeWeightDetail(obj.getWeightName(),obj.getWeightCode(),obj.getVersion());
    }

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

    @Override
    public List<ProductionCircleWeightDetail> getWeightByRecipePurpose(Long id, String recipePurpose, Integer weight,Long pId) {
        ProductionCircleWeight obj = dao1.getRecipeWeightById(id);
        if (obj != null){
            //获取重量方案详情
            List<ProductionCircleWeightDetail> list = dao.getWeightByRecipePurpose(obj.getWeightName(),obj.getWeightCode(),obj.getVersion(),recipePurpose,weight);
            //TODO 实际业务要修改
//            if (list != null){
//                //获取搭配方案详情
//                ProductionCircleDosingList productionCircleDosingList = productionCircleDosingListDao.getOneById(pId);
//                List<ProductionCircleRecipeDetail> list2 = null;
//                if (productionCircleDosingList != null){
//                    //获取配方详情
//                    ProductionCircleRecipe obj1 = productionCircleRecipeDao.getRecipeWeightById(productionCircleDosingList.getRecipeId());
//                    list2 = productionCircleRecipeDetailDao.getRecipeDetailsByIdAndPurpose(obj1.getRecipeName(),obj1.getRecipeCode(),obj1.getVersion(),recipePurpose);
//                }
//                for (ProductionCircleWeightDetail productionCircleWeightDetail : list) {
//                    productionCircleWeightDetail.setRecipePurpose(recipePurpose);
//                    if (list2 != null){
//                        for (ProductionCircleRecipeDetail productionCircleRecipeDetail : list2) {
//                            //判断配方用途是否相同
//                            if (productionCircleWeightDetail.getRecipeKind().equals(productionCircleRecipeDetail.getRecipeKind())) {
//                                productionCircleWeightDetail.setSuppliesName(productionCircleRecipeDetail.getSuppliesName());
//                                productionCircleWeightDetail.setSuppliesCode(productionCircleRecipeDetail.getSuppliesType());
//                            }
//                        }
//
//                    }
//                }
//            }
            return list;
        }
        return null;
    }

    @Override
    public List<ProductionCircleWeightDetail> getProductionCircleWeightDetailsByParam(Long weightId, String againNum) {
        return dao.getProductionCircleWeightDetailsByParam(weightId,againNum);
    }



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

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

//    Function<ProductionCircleWeightDetail, String>  ma = new Function<ProductionCircleWeightDetail, String>() {
//
//        @Override
//        @Deprecated
//        public String apply(ProductionCircleWeightDetail t) {
//            Object object = t.getGross();
//            return object.toString();
//        }
//    };
}
