package cn.yw.operationcount.videoappraisecount.service;


import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.base.bean.BaseService;
import cn.base.util.CommUtils;
import cn.base.util.StringUtil;
import cn.yw.operationcount.videoappraisecount.dao.YwcountvideoappraiseDao;
import net.sf.json.JSONObject;

@Service("ywcountvideoappraiseService")
public class YwcountvideoappraiseService extends BaseService {
	
	private static Log log = LogFactory.getLog(YwcountvideoappraiseService.class);
	
	@Autowired
	private YwcountvideoappraiseDao ywcountvideoappraiseDao;
	
	/**
	 * 保存记录
	 */
	public void insert_Ywcountvideoappraise(Map map) throws SQLException{
		CommUtils.addParams(map, false);
		ywcountvideoappraiseDao.insert_Ywcountvideoappraise(map);
	}
	public void insertWeight(Map map) throws SQLException{
		ywcountvideoappraiseDao.insertWeight(map);
	}

	
	/**
	 * 修改记录
	 */
	public Integer update_Ywcountvideoappraise(Map map) throws SQLException{
		CommUtils.validateUpdateParams(map);
		CommUtils.addParams(map, true);
		return ywcountvideoappraiseDao.update_Ywcountvideoappraise(map);
	}
	public Integer updateWeight(Map map) throws SQLException{
		return ywcountvideoappraiseDao.updateWeight(map);
	}
	
	/**
	 * 删除记录
	 */
	public Integer delete_Ywcountvideoappraise(Map map) throws SQLException{
		CommUtils.validateDeleteParams(map);
		return ywcountvideoappraiseDao.delete_Ywcountvideoappraise(map);
	}
	
	/**
	 * 批处理删除记录
	 */
	public Integer batch_delete_Ywcountvideoappraise(Map map) throws SQLException{
		return ywcountvideoappraiseDao.batch_delete_Ywcountvideoappraise(map);
	}
	
	/**
	 * 查询总数
	 */
	public Integer count_Ywcountvideoappraise(Map map) throws SQLException{
		return ywcountvideoappraiseDao.count_Ywcountvideoappraise(map);
	}
	
	/**
	 * 查询列表记录SELECT
	 */
	public List<Map> query_Ywcountvideoappraise(Map map) throws SQLException{
		return ywcountvideoappraiseDao.query_Ywcountvideoappraise(map);
	}
	
	/**
	 * 查询一条
	 */
	 
	public Map get_Ywcountvideoappraise(Map map) throws SQLException{
		return ywcountvideoappraiseDao.get_Ywcountvideoappraise(map);
	}
	public Map getWeight(Map map) throws SQLException{
		return ywcountvideoappraiseDao.getWeight(map);
	}
	
	/**
	 * 导出EXCEL
	 */
	 
	public Map exportYwcountvideoappraise(Map map) throws SQLException{
		return this.listYwcountvideoappraiseByPage(map);
	}
	
	/**
	 * Ywcountvideoappraise 分页
	 */
	public Map listYwcountvideoappraiseByPage(Map map) throws SQLException {
		List<Map> list = ywcountvideoappraiseDao.query_Ywcountvideoappraise(map);
		Integer totalCount = ywcountvideoappraiseDao.count_Ywcountvideoappraise(map);
		map.clear();
		map.put("total", totalCount);
		map.put("rows", list);
		return map;
	}
	public Map regionAssessmentCountList(Map map) throws SQLException {
		List<Map> result = new ArrayList<Map>();
		Map weightMap = ywcountvideoappraiseDao.getWeight(new HashMap());
		if(weightMap==null){
			map.clear();
			map.put("total", 0);
			map.put("rows", null);
			return map;
		}
		int monitorSum = 0,totalSum=0,vqdnormalSum=0,lalnormalSum=0,
				recorddurationnormalSum=0,norecorddurationnormalSum=0,jobtotalSum=0,jobnormalSum=0;
		double lxscSum=0.0,zxscSum = 0.0,scoreSum =0.0;
		List<Map> list = ywcountvideoappraiseDao.regionAssessmentCountList(map);
		//总计
		Map<String,Object> totalMap = new HashMap<String,Object>();
		if(list!=null&&list.size()>0){
			for(Map m : list){
				Map<String,Object> mapTemp = new HashMap<String,Object>();
				mapTemp.put("areanum", m.get("areanum"));
				mapTemp.put("areaname", m.get("areaname"));
				int total = StringUtil.str2Numeric(m.get("total")+"");
				mapTemp.put("total",total);
				double score = 0.0;
				/*if(total==0){
					map.clear();
					map.put("total", 0);
					map.put("rows", null);
					return map;
				}*/
//				map.put("areanum", m.get("areanum"));
//				Integer jksum = ywcountvideoappraiseDao.getVideoCount(map);
				int jksum = StringUtil.str2Numeric(m.get("jksum")+"");
				mapTemp.put("jksum",jksum);
				
				double zxsc = StringUtil.str2Double(m.get("zxsc")+"");
				mapTemp.put("zxsc",zxsc);
				
				double lxsc = StringUtil.str2Double(m.get("lxsc")+"");
				mapTemp.put("lxsc",lxsc);
				double zxRate = 0 ;
				if((zxsc+lxsc)>0){
					zxRate = zxsc/(zxsc+lxsc);
				}
				score += zxRate * StringUtil.str2Numeric(weightMap.get("onlineweight")+"");
				mapTemp.put("zxRate",new DecimalFormat("0.00").format(zxRate*100.0)+"%");
				
				int vqdnormalnum = StringUtil.str2Numeric(m.get("vqdnormalnum")+"");
				mapTemp.put("vqdnormalnum",vqdnormalnum);
				int lalnormalnum = StringUtil.str2Numeric(m.get("lalnormalnum")+"");
				mapTemp.put("lalnormalnum",lalnormalnum);
				if(total>0){
					double videoIntactRate = vqdnormalnum*1.0/total;
					score += videoIntactRate * StringUtil.str2Numeric(weightMap.get("videoweight")+"");
					mapTemp.put("videoIntactRate",new DecimalFormat("0.00").format(videoIntactRate*100.0)+"%");
					
					double lalIntactRate = lalnormalnum*1.0/total;
					score += lalIntactRate * StringUtil.str2Numeric(weightMap.get("lalweight")+"");
					mapTemp.put("lalIntactRate",new DecimalFormat("0.00").format(lalIntactRate*100.0)+"%");
				}else{
					mapTemp.put("videoIntactRate","0.00%");
					mapTemp.put("lalIntactRate","0.00%");
				}
				
				int recorddurationnormalnum = StringUtil.str2Numeric(m.get("recorddurationnormalnum")+"");
				int norecorddurationnormalnum = StringUtil.str2Numeric(m.get("norecorddurationnormalnum")+"");
				mapTemp.put("recorddurationnormalnum",recorddurationnormalnum);
				mapTemp.put("norecorddurationnormalnum",norecorddurationnormalnum);
				double recordIntactRate = 0;
				if((recorddurationnormalnum+norecorddurationnormalnum)>0){
					recordIntactRate = recorddurationnormalnum*1.0/(recorddurationnormalnum+norecorddurationnormalnum);
					score += recordIntactRate * StringUtil.str2Numeric(weightMap.get("recordweight")+"");
				}
				mapTemp.put("recordIntactRate",new DecimalFormat("0.00").format(recordIntactRate*100.0)+"%");
				
				int jobtotal = StringUtil.str2Numeric(m.get("jobtotal")+"");
				int jobnormalnum = StringUtil.str2Numeric(m.get("jobnormalnum")+"");
				mapTemp.put("jobtotal",jobtotal);
				mapTemp.put("jobnormalnum",jobnormalnum);
				double jobIntactRate = 0;
				if(jobtotal>0){
					jobIntactRate = jobnormalnum*1.0/jobtotal;
					score += jobIntactRate * StringUtil.str2Numeric(weightMap.get("repairweight")+"");
				}
				mapTemp.put("jobIntactRate",new DecimalFormat("0.00").format(jobIntactRate*100.0)+"%");
				mapTemp.put("score", new DecimalFormat("0.00").format(score)+"");
				result.add(mapTemp);
				totalSum += total;
				monitorSum += jksum;
				zxscSum += zxsc;
				lxscSum += lxsc;
				vqdnormalSum += vqdnormalnum;
				lalnormalSum += lalnormalnum;
				recorddurationnormalSum += recorddurationnormalnum;
				norecorddurationnormalSum += norecorddurationnormalnum;
				jobtotalSum += jobtotal;
				jobnormalSum += jobnormalnum;
				scoreSum += score;
			}
			
			totalMap.put("jksum", monitorSum);
			totalMap.put("zxscSum", zxscSum);
			totalMap.put("scoreSum", new DecimalFormat("0.00").format(scoreSum));
			if((zxscSum+lxscSum)>0){
				totalMap.put("zxscRate", new DecimalFormat("0.00").format(zxscSum*100.0/(zxscSum+lxscSum))+"%");
			}else{
				totalMap.put("zxscRate","0.00%");
			}
			if(totalSum>0){
				totalMap.put("videoRate", new DecimalFormat("0.00").format((vqdnormalSum*100.0/totalSum))+"%");
				totalMap.put("lalRate", new DecimalFormat("0.00").format((lalnormalSum*100.0/totalSum))+"%");
			}else{
				totalMap.put("videoRate","0.00%");
				totalMap.put("lalRate","0.00%");
			}
			
			if((recorddurationnormalSum+norecorddurationnormalSum)>0){
				totalMap.put("recordRate", new DecimalFormat("0.00").format((recorddurationnormalSum*100.0/(recorddurationnormalSum+norecorddurationnormalSum)))+"%");
			}else{
				totalMap.put("recordRate","0.00%");
			}
			if(jobtotalSum>0){
				totalMap.put("recordRate", new DecimalFormat("0.00").format((jobnormalSum*100.0/jobtotalSum))+"%");
			}else{
				totalMap.put("jobRate","0.00%");
			}
//			result.add(totalMap);
		}
		Map[] maps = new Map[result.size()];
		maps= result.toArray(maps);
		for(int i=0;i<maps.length;i++){
			for(int j=i+1;j<maps.length;j++){
				if((maps[i].get("score")+"").compareTo((maps[j].get("score")+""))<0){
					Map t = maps[i];
					maps[i] = maps[j];
					maps[j] = t;
				}
			}
		}
		Map[] newMaps = Arrays.copyOf(maps, maps.length+1);
		newMaps[newMaps.length-1] = totalMap;
		map.clear();
		map.put("total", maps.length);
		map.put("rows", newMaps);
		return map;
	}
	public Map regionAssessmentCountListByRegion(Map map) throws SQLException {
		List<Map> result = new ArrayList<Map>();
		Map weightMap = ywcountvideoappraiseDao.getWeight(new HashMap());//获取权重
		int monitorSum = 0,totalSum=0,vqdnormalSum=0,lalnormalSum=0,
				recorddurationnormalSum=0,norecorddurationnormalSum=0,jobtotalSum=0,jobnormalSum=0,
				scoreSum =0;
		double lxscSum=0.0,zxscSum = 0.0;
		List<Map> list = ywcountvideoappraiseDao.regionAssessmentCountListByRegion(map);//获取数据
		Integer zs = ywcountvideoappraiseDao.count_regionAssessmentCountListByRegion(map);//获取总数数据
		if(list!=null&&list.size()>0){//计算得分等
			for(Map m : list){
				Map<String,Object> mapTemp = new HashMap<String,Object>();
				mapTemp.put("areanum", m.get("areanum"));
				mapTemp.put("areaname", m.get("areaname"));
				mapTemp.put("devicenum", m.get("devicenum"));
				mapTemp.put("channelnum", m.get("channelnum"));
				mapTemp.put("videoname", m.get("videoname"));
				int total = StringUtil.str2Numeric(m.get("total")+"");//总数
				mapTemp.put("total",total);
				double score = 0.0;
				if(total==0){
					map.clear();
					map.put("total", 0);
					map.put("rows", null);
					return map;
				}
				/*int jksum = StringUtil.str2Numeric(m.get("jksum")+"");
				mapTemp.put("jksum",jksum);*/
				
				double zxsc = StringUtil.str2Double(m.get("zxsc")+"");
				mapTemp.put("zxsc",zxsc);
				
				double lxsc = StringUtil.str2Double(m.get("lxsc")+"");
				mapTemp.put("lxsc",lxsc);
				double zxRate = 0.0;
				if((zxsc+lxsc)>0){
					zxRate = zxsc/(zxsc+lxsc);
				}
				score += zxRate * StringUtil.str2Numeric(weightMap.get("onlineweight")+"");
				mapTemp.put("zxRate",new DecimalFormat("0.00").format(zxRate*100.0)+"%");
				
				int vqdnormalnum = StringUtil.str2Numeric(m.get("vqdnormalnum")+"");
				mapTemp.put("vqdnormalnum",vqdnormalnum);
				double videoIntactRate = vqdnormalnum*1.0/total;
				score += videoIntactRate * StringUtil.str2Numeric(weightMap.get("videoweight")+"");
				mapTemp.put("videoIntactRate",new DecimalFormat("0.00").format(videoIntactRate*100.0)+"%");
				
				int lalnormalnum = StringUtil.str2Numeric(m.get("lalnormalnum")+"");
				mapTemp.put("lalnormalnum",lalnormalnum);
				double lalIntactRate = lalnormalnum*1.0/total;
				score += lalIntactRate * StringUtil.str2Numeric(weightMap.get("lalweight")+"");
				mapTemp.put("lalIntactRate",new DecimalFormat("0.00").format(lalIntactRate*100.0)+"%");
				
//				int recordnormalnum = StringUtil.str2Numeric(m.get("recordnormalnum")+"");
				
				
				int recorddurationnormalnum = StringUtil.str2Numeric(m.get("recorddurationnormalnum")+"");
				int norecorddurationnormalnum = StringUtil.str2Numeric(m.get("norecorddurationnormalnum")+"");
				mapTemp.put("recorddurationnormalnum",recorddurationnormalnum);
				mapTemp.put("norecorddurationnormalnum",norecorddurationnormalnum);
				double recordIntactRate = 0;
				if((recorddurationnormalnum+norecorddurationnormalnum)!=0){
					recordIntactRate = recorddurationnormalnum*1.0/(recorddurationnormalnum+norecorddurationnormalnum);
					score += recordIntactRate * StringUtil.str2Numeric(weightMap.get("recordweight")+"");
				}
				mapTemp.put("recordIntactRate",new DecimalFormat("0.00").format(recordIntactRate*100.0)+"%");
				
				int jobtotal = StringUtil.str2Numeric(m.get("jobtotal")+"");
				int jobnormalnum = StringUtil.str2Numeric(m.get("jobnormalnum")+"");
				mapTemp.put("jobtotal",jobtotal);
				mapTemp.put("jobnormalnum",jobnormalnum);
				double jobIntactRate = 0;
				if(jobtotal!=0){
					jobIntactRate = jobnormalnum*1.0/jobtotal;
					score += jobIntactRate * StringUtil.str2Numeric(weightMap.get("repairweight")+"");
				}
				mapTemp.put("jobIntactRate",new DecimalFormat("0.00").format(jobIntactRate*100.0)+"%");
				mapTemp.put("score", new DecimalFormat("0.00").format(score)+"");
				result.add(mapTemp);
			}
		}
		Map[] maps = new Map[result.size()];
		maps= result.toArray(maps);
		for(int i=0;i<maps.length;i++){
			for(int j=i+1;j<maps.length;j++){
				if((maps[i].get("score")+"").compareTo((maps[j].get("score")+""))<0){
					Map t = maps[i];
					maps[i] = maps[j];
					maps[j] = t;
				}
			}
		}
		map.clear();
		map.put("total", zs);
		map.put("rows", maps);
		return map;
	}
	
	// 点位录像情况统计                           /start   
	public List query_dwlx(Map map) throws SQLException{
		return (List) ywcountvideoappraiseDao.query_dwlx(map);
	}
	public Integer count_dwlx(Map map) throws SQLException{
		return (Integer) ywcountvideoappraiseDao.count_dwlx(map);
	}
//	统计 柱状图
	public List show_bar(Map map) throws SQLException{
		return (List) ywcountvideoappraiseDao.show_bar(map);
	}
	
	// 点位录像情况统计                           /end  
	
	
	
	
	public static void main(String[] args) {
		System.out.println(1.0*1==1);
	}
	
}
