/**
 * 版权所有 ©2021 莱欧有限公司
 * 工程名：yearBooks
 * 文件名：MainShowService.java
 * 创建日期：2021年10月28日 下午4:03:17
 * 作者：mt.W
 */
package com.lion.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lion.dao.MainShowDao;
import com.lion.dao.OrganizationTreeDao;
import com.lion.util.Util;

import jodd.util.StringUtil;


/**
 * 类描述
 * @author mt.W
 * @since 2021年10月28日
 */
@RestController
@RequestMapping("mainshow")
public class MainShowService {

	@Autowired
	private MainShowDao dao;
	@Autowired
	private OrganizationTreeDao oDao;
	
	public List<Map<String, Object>> getOrgdeptList(){
		List<Map<String,Object>> list = oDao.getParentNode(2);
		return list;
	}
	public Map<String,Object> getNongYeShengChanZongZhiInfo(String tableName,String colNumName,String year){
		List<Map<String,Object>> orgdept = getOrgdeptList();
		String cols[] = colNumName.split(",");
		List<Map<String,Object>> record = dao.getItemOrgdeptRecord(tableName, year);
		
		Map<String,Object> MapLeftMap = new HashMap<String, Object>();
		
		Map<String,Object> recordMap = new HashMap<String, Object>();
		for(String s:cols){
			if(StringUtil.isNotEmpty(s)){
				BigDecimal sr = new  BigDecimal("0.0");
				for(Map<String,Object> r:record){
					sr = sr.add(new BigDecimal(r.get(s).toString()));
				}
				MapLeftMap.put(s, sr);
			}
		}
		for(Map<String,Object> r:record){
			String c = cols[0];
			recordMap.put(r.get("orgid").toString(), r.get(c));
		}
		List<String> orgName = new ArrayList<String>();
		List<String> orgCount = new ArrayList<String>();
		for(Map<String,Object> o:orgdept){
			orgName.add(o.get("text").toString());
			if(recordMap.get(o.get("id").toString())==null){
				orgCount.add("0");
			}else{
				orgCount.add(recordMap.get(o.get("id").toString()).toString());
			}
		}
		List<Map<String,Object>> sumRecord =  dao.getItemOrgdeptSumItem(tableName, cols[0]);
		List<String> years = new ArrayList<String>();
		List<String> sumCount = new ArrayList<String>();
		for(Map<String,Object> s:sumRecord){
			years.add(s.get("year").toString());
			sumCount.add(s.get("count").toString());
		}
		if(record.size()>0){
			Collections.sort(record, new Comparator<Map<String, Object>>() {
	            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
	            	String count_1 = o1.get(cols[0]).toString();
	            	String count_2=o2.get(cols[0]).toString();
	            	if(StringUtil.isEmpty(count_1)){
	            		count_1 = "0";
	            	}
	            	if(StringUtil.isEmpty(count_2)){
	            		count_2 = "0";
	            	}
	                Integer name1 = Integer.valueOf((int) (Double.valueOf(count_1)*100)) ;//name1是从你list里面拿出来的一个 
	                Integer name2 = Integer.valueOf((int) (Double.valueOf(count_2)*100)); //name1是从你list里面拿出来的第二个name
	                return name2.compareTo(name1);
	            }
	        });
		}
		
		
		Map<String,Object> map =  Util.responseMap("true",record);
		map.put("MapLeftMap", MapLeftMap);
		map.put("orgName", orgName);
		map.put("orgCount", orgCount);
		map.put("years", years);
		map.put("sumCount", sumCount);
		map.put("orgdeptCount", orgdept.size());
		return map;
	}
	public Map<String,Object> getMainShowSgInfo(String fruitId,String type,String year){
		List<Map<String,Object>> orgdept = getOrgdeptList();
		List<Map<String,Object>> record = dao.getFruitRecord(year);
		Map<String,List<Map<String,Object>>> recordMap = new HashMap<String, List<Map<String,Object>>>();
		
		BigDecimal bzzmj = new  BigDecimal("0.0");
		BigDecimal zcl = new  BigDecimal("0.0");
		for(Map<String,Object> r:record){
			if(recordMap.get(r.get("orgid").toString())==null){
				List<Map<String,Object>> temp = new ArrayList<Map<String,Object>>();
				temp.add(r);
				recordMap.put(r.get("orgid").toString(),temp);
			}else{
				recordMap.get(r.get("orgid").toString()).add(r);
			}
		}
		List<String> orgName = new ArrayList<String>();
		List<String> orgCount = new ArrayList<String>();
		for(Map<String,Object> o:orgdept){
			orgName.add(o.get("text").toString());
			if(recordMap.get(o.get("id").toString())!=null){
				List<Map<String,Object>> temp = recordMap.get(o.get("id").toString());
				BigDecimal sr = new  BigDecimal("0.0");
				for(Map<String,Object> r:temp){
					if(StringUtil.isNotEmpty(fruitId)){
						if(r.get("typeId").toString().equals(fruitId)){
							sr = sr.add(new BigDecimal(r.get(type).toString()));
							bzzmj=bzzmj.add(new BigDecimal(r.get("bzmj").toString()));
							zcl=zcl.add(new BigDecimal(r.get("zcl").toString()));
						}
					}else{
						sr = sr.add(new BigDecimal(r.get(type).toString()));
						bzzmj=bzzmj.add(new BigDecimal(r.get("bzmj").toString()));
						zcl=zcl.add(new BigDecimal(r.get("zcl").toString()));
					}
				}
				o.put("count", sr);
				orgCount.add(sr+"");
			}else{
				orgCount.add("0");
				o.put("count", "0");
			}
		}
		List<Map<String,Object>> sumRecord = dao.getItemOrgdeptSumItem("zzy_sg", type,fruitId);
		
		List<String> years = new ArrayList<String>();
		List<String> sumCount = new ArrayList<String>();
		for(Map<String,Object> s:sumRecord){
			years.add(s.get("year").toString());
			sumCount.add(s.get("count").toString());
		}
		
		Collections.sort(orgdept, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
            	String count_1 = o1.get("count").toString();
            	String count_2=o2.get("count").toString();
            	if(StringUtil.isEmpty(count_1)){
            		count_1 = "0";
            	}
            	if(StringUtil.isEmpty(count_2)){
            		count_2 = "0";
            	}
                Integer name1 = Integer.valueOf((int) (Double.valueOf(count_1)*100)) ;//name1是从你list里面拿出来的一个 
                Integer name2 = Integer.valueOf((int) (Double.valueOf(count_2)*100)); //name1是从你list里面拿出来的第二个name
                return name2.compareTo(name1);
            }
        });
		Map<String,Object> map = Util.responseMap("true",orgdept);
		map.put("danwei", type.equals("bzmj")?"亩":"吨");
		map.put("bzzmj", bzzmj);
		map.put("orgName", orgName);
		map.put("orgCount", orgCount);
		map.put("zcl", zcl);
		map.put("years", years);
		map.put("sumCount", sumCount);
		return map;
 	}
}
