package com.huimv.produce.produce.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.ProdProduce;
import com.huimv.produce.produce.mapper.ProdProduceMapper;
import com.huimv.produce.produce.service.IProdProduceService;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-11-25
 */
@Service
public class ProdProduceServiceImpl extends ServiceImpl<ProdProduceMapper, ProdProduce> implements IProdProduceService {

    @Override
    public Result listImportance(Map<String, String> map) {
        String farmId = map.get("farmId");
        String type = map.get("type");
        String startDate = map.get("startDate");
        String endtDate = map.get("endtDate");
        QueryWrapper<ProdProduce> wrapper = new QueryWrapper<>();
        wrapper.eq("farm_id",farmId);
        if (StringUtils.isNotBlank(startDate)){
            Integer year = Integer.parseInt(startDate.substring(0,4));
            Integer month = Integer.parseInt(startDate.substring(5,7));
            wrapper.ge("year",year);
            wrapper.ge("month",month);
        }
        if (StringUtils.isNotBlank(endtDate)){
            Integer year = Integer.parseInt(endtDate.substring(0,4));
            Integer month = Integer.parseInt(endtDate.substring(5,7));
            wrapper.le("year",year);
            wrapper.le("month",month);
        }
        List<String> stockTypes = new ArrayList();
        List<ProdProduce> prodProduces = new ArrayList<>();
        // 成活率：产房，保育，肥猪
        Map endMap = new HashMap();
        if ("4".equals(type)){
            stockTypes.add("grice_rate");
            stockTypes.add("piglet_rate");
            stockTypes.add("fatpig_rate");
            wrapper.in("stock_type",stockTypes);


            prodProduces = this.list(wrapper);
            List priceTime = new ArrayList();
            List priceValue = new ArrayList();

            List pigletTime = new ArrayList();
            List pigletValue = new ArrayList();

            List fatpigTime = new ArrayList();
            List fatpigValue = new ArrayList();


            for (ProdProduce prodProduce : prodProduces) {
                String stockType = prodProduce.getStockType();
                String quantity = prodProduce.getStockQuantity();
                String time =  prodProduce.getYear()+"-"+ prodProduce.getMonth() ;

                if ("grice_rate".equals(stockType)){
                    priceValue.add(toBaifenbi(quantity));
                    priceTime.add(time);

                }
                if ("piglet_rate".equals(stockType)){
                    pigletValue.add(toBaifenbi(quantity));
                    pigletTime.add(time);
                }
                if ("fatpig_rate".equals(stockType)){
                    fatpigValue.add(toBaifenbi(quantity));
                    fatpigTime.add(time);
                }
            }

            Map map1 = new HashMap();
            map1.put("value",priceValue);
            map1.put("time",priceTime);
            map1.put("isPercentage",1);
            endMap.put("grice_rate",map1);

            Map map2 = new HashMap();
            map2.put("value",pigletValue);
            map2.put("time",pigletTime);
            map2.put("isPercentage",1);
            endMap.put("piglet_rate",map2);

            Map map3 = new HashMap();
            map3.put("value",fatpigValue);
            map3.put("time",fatpigTime);
            map3.put("isPercentage",1);
            endMap.put("fatpig_rate",map3);

            endMap.put("isPercentage",1);
            endMap.put("isWeight",0);
            endMap.put("isHead",0);
        }
        // 断奶：平均断奶窝重，窝断奶仔猪数
        else if ("3".equals(type)){
            stockTypes.add("avg_weight");
            stockTypes.add("break_grice");
            wrapper.in("stock_type",stockTypes);
            prodProduces = this.list(wrapper);

            List priceTime = new ArrayList();
            List priceValue = new ArrayList();

            List pigletTime = new ArrayList();
            List pigletValue = new ArrayList();


            for (ProdProduce prodProduce : prodProduces) {
                String stockType = prodProduce.getStockType();
                String quantity = prodProduce.getStockQuantity();
                String time =  prodProduce.getYear()+"-"+ prodProduce.getMonth() ;
                if ("avg_weight".equals(stockType)){
                    priceValue.add(quantity);
                    priceTime.add(time);

                }
                if ("break_grice".equals(stockType)){
                    pigletValue.add(quantity);
                    pigletTime.add(time);
                }
            }

            Map map1 = new HashMap();
            map1.put("value",priceValue);
            map1.put("time",priceTime);
            map1.put("isPercentage",2);
            endMap.put("avg_weight",map1);

            Map map2 = new HashMap();
            map2.put("value",pigletValue);
            map2.put("time",pigletTime);
            map2.put("isPercentage",0);
            endMap.put("break_grice",map2);

            endMap.put("isPercentage",0);
            endMap.put("isWeight",1);
            endMap.put("isHead",1);


        }
        //   分娩：平均窝产（3）
        else if ("2".equals(type)){
            stockTypes.add("avg_farrow_1");
            stockTypes.add("avg_farrow_2");
            stockTypes.add("avg_farrow_3");
            wrapper.in("stock_type",stockTypes);
            prodProduces = this.list(wrapper);
            List priceTime = new ArrayList();
            List priceValue = new ArrayList();

            List pigletTime = new ArrayList();
            List pigletValue = new ArrayList();

            List fatpigTime = new ArrayList();
            List fatpigValue = new ArrayList();


            for (ProdProduce prodProduce : prodProduces) {
                String stockType = prodProduce.getStockType();
                String quantity = prodProduce.getStockQuantity();
                String time =  prodProduce.getYear()+"-"+ prodProduce.getMonth() ;
                if ("avg_farrow_1".equals(stockType)){
                    priceValue.add(quantity);
                    priceTime.add(time);

                }
                if ("avg_farrow_2".equals(stockType)){
                    pigletValue.add(quantity);
                    pigletTime.add(time);
                }
                if ("avg_farrow_3".equals(stockType)){
                    fatpigValue.add(quantity);
                    fatpigTime.add(time);
                }
            }

            Map map1 = new HashMap();
            map1.put("value",priceValue);
            map1.put("time",priceTime);
            map1.put("isPercentage",0);
            endMap.put("avg_farrow_1",map1);

            Map map2 = new HashMap();
            map2.put("value",pigletValue);
            map2.put("time",pigletTime);
            map2.put("isPercentage",0);
            endMap.put("avg_farrow_2",map2);

            Map map3 = new HashMap();
            map3.put("value",fatpigValue);
            map3.put("time",fatpigTime);
            map3.put("isPercentage",0);
            endMap.put("avg_farrow_3",map3);

            endMap.put("isPercentage",0);
            endMap.put("isWeight",0);
            endMap.put("isHead",1);
        }
        // 配种：七日，
        else {
            stockTypes.add("break_rate");
            wrapper.in("stock_type",stockTypes);
            prodProduces = this.list(wrapper);

            List priceTime = new ArrayList();
            List priceValue = new ArrayList();

            for (ProdProduce prodProduce : prodProduces) {
                String stockType = prodProduce.getStockType();
                String quantity = prodProduce.getStockQuantity();
                String time =  prodProduce.getYear()+"-"+ prodProduce.getMonth() ;
                if ("break_rate".equals(stockType)){
                    priceValue.add(toBaifenbi(quantity));
                    priceTime.add(time);

                }

            }

            Map map1 = new HashMap();
            map1.put("value",priceValue);
            map1.put("time",priceTime);
            map1.put("isPercentage",1);
            endMap.put("break_rate",map1);
            endMap.put("isPercentage",1);
            endMap.put("isWeight",0);
            endMap.put("isHead",0);
        }


        return new Result(ResultCode.SUCCESS,endMap);
    }

    private String toBaifenbi(String quantity) {

        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(Double.parseDouble(quantity) * 100) ;

    }



}
