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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.produce.produce.entity.ProdImportantIndicatorEntity;
import com.huimv.produce.produce.entity.ProdIndicatorDynamicEntity;
import com.huimv.produce.produce.repo.ProdImportantIndicatoRepo;
import com.huimv.produce.produce.repo.ProdIndicatorDynamicRepo;
import com.huimv.produce.produce.service.IImportantDynamicService;
import com.huimv.produce.produce.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@Service
public class ImportantDynamicServiceImpl implements IImportantDynamicService {
    @Autowired
    private DateUtil dateUtil;
    @Autowired
    private ProdIndicatorDynamicRepo indicatorDynamicRepo;
    @Autowired
    private ProdImportantIndicatoRepo importantIndicatoRepo;

    @Override
    public Result getImportantDynamicForIndex(Integer farmId) {
        //以月为时间单位，进行统计。
        String year = dateUtil.getThisYear();
        //正式月份
        // String month = dateUtil.getThisMonth();
        //演示月份
        String month = "2";
        System.out.println("year>>" + year);
        System.out.println("month>>" + month);

        //查询动态统计表
        List<ProdIndicatorDynamicEntity> dynamicEntityList = getDynamicCount(farmId,year,month);

        //查询一周数据
        JSONArray importantJa = getOneWeekImportant(farmId,year,month);
        System.out.println("11 importantJa.size>>" + importantJa.size());
//        for(int a=0;a<importantJa.size();a++){
//            JSONObject importantJo = importantJa.getJSONObject(a);
//            System.out.println("importantJo>>"+importantJo);
//            if(importantJo.getString("titleNum").indexOf("%") != -1){
//                importantJo.put("titleNum",importantJo.getString("titleNum").replace("%",""));
//                importantJo.put("subtitleNum",importantJo.getString("subtitleNum").replace("%",""));
//                JSONObject dataJo = importantJo.getJSONObject("data");
//                dataJo.
//
//            }
//            importantJo.getString("");
//
//        }

        JSONObject outJo = new JSONObject();
        outJo.put("count", dynamicEntityList);
        outJo.put("list", importantJa);

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

    //查询重要指标一周统计
    private JSONArray getOneWeekImportant(Integer farmId,String year,String month) {
        JSONArray outJa = new JSONArray();
        //
        JSONArray titleJa = getTitle();

        //查询重要指标详细数据
        List<ProdImportantIndicatorEntity> importantList = importantIndicatoRepo.findAllByFarmIdAndStartAndEndDate(farmId, year, Integer.parseInt(month));
        for (int a = 0; a < titleJa.size(); a++) {
            JSONObject titleJo = titleJa.getJSONObject(a);
            List timeList = new ArrayList();
            List dataList = new ArrayList();
            JSONObject newJo = new JSONObject();
            outJa.add(newJo);
            newJo.put("title",titleJo.getString("title"));
            newJo.put("subtitle",titleJo.getString("subtitle"));
            newJo.put("percent",titleJo.getBoolean("percent"));
            for (ProdImportantIndicatorEntity importantEntity : importantList) {
                if (titleJo.getString("type").trim().equalsIgnoreCase(importantEntity.getStockType())) {
                    if (importantEntity.getYear() == Integer.parseInt(year) && importantEntity.getMonth() == Integer.parseInt(month)) {
                        newJo.put("titleNum", importantEntity.getStockQuantity().replace("%",""));
                        if (importantEntity.getRiseFall() > 0) {
                            newJo.put("ifPositive", true);
                            newJo.put("subtitleNum", "+"+importantEntity.getRange1().replace("%",""));
                            System.out.println(">0");
                        } else {
                            newJo.put("ifPositive", false);
                            newJo.put("subtitleNum", "-"+importantEntity.getRange1().replace("%",""));
                            System.out.println("<0");
                        }
                    }else{
                        newJo.put("titleNum", importantEntity.getStockQuantity().replace("%",""));
                        newJo.put("ifPositive", true);
                        newJo.put("subtitleNum", "+" + importantEntity.getRange1().replace("%",""));
                    }
                    timeList.add(importantEntity.getMonth());
                    dataList.add(importantEntity.getStockQuantity().replace("%",""));
                }else{
//                    !titleJo.getString("type").trim().equalsIgnoreCase(importantEntity.getStockType())
                }
            }
            JSONObject dataJo = new JSONObject();
            dataJo.put("timeList",timeList);
            dataJo.put("dataList",dataList);
            newJo.put("data",dataJo);
        }
        return outJa;
    }

    private JSONArray getTitle() {
        JSONArray titleJa = new JSONArray();
        JSONObject psyJo = new JSONObject();
        titleJa.add(psyJo);
        psyJo.put("title", "PSY");
        psyJo.put("type", "psy");
        psyJo.put("subtitle", "衡量母猪群繁殖性能力指标,一头母猪一年提供的成活仔猪数");
        psyJo.put("percent", false);

        JSONObject avgFarrow1Jo = new JSONObject();
        titleJa.add(avgFarrow1Jo);
        avgFarrow1Jo.put("title", "平均窝产仔猪");
        avgFarrow1Jo.put("type", "avg_farrow_1");
        avgFarrow1Jo.put("subtitle", "母猪每一台生产的仔猪数量");
        avgFarrow1Jo.put("percent", false);

        JSONObject griceRateJo = new JSONObject();
        titleJa.add(griceRateJo);
        griceRateJo.put("title", "产房成活率");
        griceRateJo.put("type", "grice_rate");
        griceRateJo.put("subtitle", "每一窝仔猪断奶前平均存活数量");
        griceRateJo.put("percent", true);

        JSONObject pigletRateJo = new JSONObject();
        titleJa.add(pigletRateJo);
        pigletRateJo.put("title", "保育成活率");
        pigletRateJo.put("type", "piglet_rate");
        pigletRateJo.put("subtitle", "保育阶段的猪只存活率");
        pigletRateJo.put("percent", true);

        return titleJa;
    }

    //查询动态统计数据
    private List<ProdIndicatorDynamicEntity> getDynamicCount(Integer farmId,String year,String month) {
//        String thisYear = dateUtil.getThisYear();
//        String thisMonth = dateUtil.getThisMonth();
        ProdIndicatorDynamicEntity findDynamicEntity = new ProdIndicatorDynamicEntity();
        findDynamicEntity.setFarmId(farmId);
        findDynamicEntity.setYear(Integer.parseInt(year));
        findDynamicEntity.setMonth(Integer.parseInt(month));
        Example<ProdIndicatorDynamicEntity> findExample = Example.of(findDynamicEntity);
        return indicatorDynamicRepo.findAll(findExample);
    }
}
