package com.xintong.visualinspection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.pojo.StatisiticPojo;
import com.xintong.visualinspection.pojo.StatisticExUser;
import com.xintong.visualinspection.pojo.UserRankingData;
import com.xintong.visualinspection.util.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.visualinspection.service.StatisticsService;
import com.xintong.visualinspection.util.CodeUtil;

/**
 * 文件名：StatisticsController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

	@Autowired
	private StatisticsService statisticsService;
    
    /**
     * 温馨服务考核情况反馈表（所有员工排名）
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/employee/order")
    public String getEmployeeOrder(@RequestBody StatisticsBean obj){
    	List<StatisticsBo> lists=  statisticsService.getEmployeeCheckedInfo(obj);
    	List<UserChangeStation> userChangeStations = statisticsService.getUserChangeStation(obj);
    	Map<String,Object> m = new HashMap<>();
    	m.put("lists",lists);
        m.put("userChangeStations",userChangeStations);
    	return super.returnSuccessResult(m);
    }

    @RequestMapping(value = "/employee/order/data")
    public String getEmployeeOrder(@RequestParam(required = false) String data){

        String res = "";
        if(data!=null){
            String[] items = data.split("\\$");
            //写
            if(items.length>1){
                CacheUtil.commonDataMap.put(items[0],items[1]);

            }else{
                //读
                res = CacheUtil.commonDataMap.get(items[0]);
            }
        }
        return super.returnSuccessResult((Object) res);
    }


    /**
     * 获取用户排名详情
     * @param obj
     * @return
     */
    @RequestMapping(value = "/employee/order/detail")
    public String getEmployeeOrderByUserId(@RequestBody StatisticsBean obj){
        UserRankingData userRankingData=  statisticsService.getEmployeeRankingData(obj);
        return super.returnSuccessResult(userRankingData);
    }

    

    /**
     * 温馨服务检查千分考核扣分情况汇总表
     * @param obj
     * @return
     */
    @RequestMapping(value = "/score/info")
    public String getScoreInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getFeeStationCheckedScore(obj);
    	int totalMan=0,totalcheckedMan=0,totalcheckedCount=0,total0=0,total15=0,total50=0,totalOver50=0,totalAll = 0;
    	for(StatisticsBean bean:lists){
    		totalMan+=bean.getPeople_num();
    		totalcheckedMan+=bean.getChecked_people_num();
    		totalcheckedCount+=bean.getChecked_num();
    		total0+=(bean.getChecked_people_num()-bean.getScore_fifteen()-bean.getScore_fifty()-bean.getScore_over_fifty());
    		total15+=bean.getScore_fifteen();
    		total50+=bean.getScore_fifty();
    		totalOver50+=bean.getScore_over_fifty();
    		totalAll+=bean.getAll_check_score();
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	resultMap.put("list", lists);
    	resultMap.put("totalMan", totalMan);
    	resultMap.put("totalcheckedMan", totalcheckedMan);
    	resultMap.put("totalcheckedCount", totalcheckedCount);
    	resultMap.put("total0", total0);
    	resultMap.put("total15", total15);
    	resultMap.put("total50", total50);
    	resultMap.put("totalOver50", totalOver50);
    	resultMap.put("totalAll", totalAll);
    	return super.returnSuccessResult(resultMap);
    }
    
    /**
     * 温馨服务检查情况统计表
     */
    @RequestMapping(value = "/score/item/info")
    public String getScoreItemInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getFeeStationCheckItemScore(obj);
    	int totalMan=0,totalcheckedMan=0,totalcheckedCount=0,totalAll=0;
    	Map<String,Integer> scoreTypeMap = new HashMap<String,Integer>();
    	for(StatisticsBean bean:lists){
    		totalMan+=bean.getPeople_num();
    		totalcheckedMan+=bean.getChecked_people_num();
    		totalcheckedCount+=bean.getChecked_num();
    		totalAll+=bean.getAll_check_score();
    		List<String> names = bean.getChecked_socre_name();
    		List<Integer> scores = bean.getChecked_score();
    		for(int i=0;i<names.size();i++){
    			Integer score = scoreTypeMap.get(names.get(i));
    			if(score==null) score=0;
    			score+=scores.get(i);
    			if(names.get(i)!=null){
    				scoreTypeMap.put(names.get(i), score);    				
    			}
    		}
//    		bean.get
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	resultMap.put("list", lists);
    	resultMap.put("totalMan", totalMan);
    	resultMap.put("totalcheckedMan", totalcheckedMan);
    	resultMap.put("totalcheckedCount", totalcheckedCount);
    	resultMap.put("totalAll", totalAll);
    	resultMap.putAll(scoreTypeMap);
//    	resultMap.put("total50", total50);
//    	resultMap.put("totalOver50", totalOver50);
//    	resultMap.put("totalAll", totalAll);
    	return super.returnSuccessResult(resultMap);
    }
    /**
     * 运营管理综合检查情况统计表
     */
    @RequestMapping(value = "/operation/manage/integration")
    public String getOperationInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists= statisticsService.getOperationInfo(obj);
    	int totalMan=0,totalCompanyNum=0,totalCompanyScore=0,totalThirdNum=0,
    			totalThirdScore=0,totalSpecialNum=0,totalSpecialScore=0,totalOnsiteNum=0,
    			totalOnsiteScore=0,totalDataNum=0,totalDataScore=0;
    	for(StatisticsBean bean:lists){
    		totalMan+=bean.getPeople_num();
    		totalCompanyNum += bean.getCompany_check_num();
    		totalCompanyScore += bean.getCompany_total_score();
    		totalThirdNum += bean.getThird_check_num();
    		totalThirdScore += bean.getThird_total_score();
    		totalSpecialNum += bean.getSpecial_check_num();
    		totalSpecialScore += bean.getSpecial_total_score();
    		totalOnsiteNum += bean.getOnsite_check_num();
    		totalOnsiteScore += bean.getOnsite_total_score();
    		totalDataNum += bean.getData_check_num();
    		totalDataScore += bean.getData_total_score();
    	}
    	Map<String,Object> resultMap = new HashMap<String,Object>();
    	resultMap.put("list", lists);
    	resultMap.put("totalMan", totalMan);
    	resultMap.put("totalCompanyNum", totalCompanyNum);
    	resultMap.put("totalCompanyScore", totalCompanyScore);
    	resultMap.put("totalThirdNum", totalThirdNum);
    	resultMap.put("totalThirdScore", totalThirdScore);
    	resultMap.put("totalSpecialNum", totalSpecialNum);
    	resultMap.put("totalSpecialScore", totalSpecialScore);
    	resultMap.put("totalOnsiteNum", totalOnsiteNum);
    	resultMap.put("totalOnsiteScore", totalOnsiteScore);
    	resultMap.put("totalDataNum", totalDataNum);
    	resultMap.put("totalDataScore", totalDataScore);
    	return super.returnSuccessResult(resultMap);
    }
    /**
     * 12个月考核等分总体
     */
    @RequestMapping(value = "/score/year/info")
    public String selectYearScoreChange(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getYearScoreChange(obj);
    	return super.returnSuccessResult(lists);
    }


    /**
     * 收费站
     */
    @RequestMapping(value = "/score/tollgate/info")
    public String selectTollgateScoreChange(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getFeeStationScoreInfo(obj);
    	return super.returnSuccessResult(lists);
    }
    
    /**
     * 道管中心3
     */
    @RequestMapping(value = "/score/center/manage/info")
    public String selectCentermanageScoreChange(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getCenterManageScoreInfo(obj);
    	return super.returnSuccessResult(lists);
    }
    
    /**
     * 道管下收费站
     */
    @RequestMapping(value = "/score/manage/tollgate/info")
    public String selectCentermanageTollgateScoreChange(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getSingleFeeStationScoreInfo(obj);
    	return super.returnSuccessResult(lists);
    }
    
    
    /**
     * 考核扣分情况比
     */
    @RequestMapping(value = "/lost/score/rate")
    public String getCheckedInfoRate(@RequestBody StatisticsBean obj){
    	StatisticsBean returnObj=  statisticsService.getCheckedScoreInfo(obj);
    	return super.returnSuccessResult(returnObj);
    }
    
    /**
     * 考核扣分项情况比
     */
    @RequestMapping(value = "/lost/itemscore/rate")
    public String getCheckedItemInfoRate(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> returnList=  statisticsService.getCheckedItemScoreInfo(obj);
    	return super.returnSuccessResult(returnList);
    }
    
    /**
     * 考核扣分项两次占比
     */
    @RequestMapping(value = "/lost/twice/itemscore/rate")
    public String getTwiceCheckedItemInfoRate(@RequestBody StatisticsBean obj){
    	Map<String,List<StatisticsBean>> returnMap=  statisticsService.get2CheckedItemScoreInfo(obj);
    	return super.returnSuccessResult(returnMap);
    }
    
    /**
     * 获取导管中心前10名,后10名 或者收费站排名
     */
    @RequestMapping(value = "/stuff/score/info")
    public String getPersonalStuffItemInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> returnList=  statisticsService.getPersonalScoreInfo(obj);
    	return super.returnSuccessResult(returnList);
    }
    
    /**
     * 获取收费站排名前5名
     */
    @RequestMapping(value = "/fs/up/five")
    public String getFsUp5PersonInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> returnList=  statisticsService.getFSUp5Person(obj);
    	return super.returnSuccessResult(returnList);
    }
    
    /**
     * 获取收费站中人的12月情况
     */
    @RequestMapping(value = "/one/person/year/info")
    public String getFsOnePersonInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> returnList=  statisticsService.getOneFSCheckedInfo(obj);
    	return super.returnSuccessResult(returnList);
    }
    
    /**
     * 获取收费站各项扣分情况
     */
    @RequestMapping(value = "/one/person/checkedItem/info")
    public String getFsOnePersonCheckedItemInfo(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> returnList=  statisticsService.getOneCheckedItemInfo(obj);
    	return super.returnSuccessResult(returnList);
    }
    
    /**
     * 获取收费站具体某个人的扣分详情
     */
    @RequestMapping(value = "/one/checkedItem/detail/info/{page}/{size}")
    public String getFsOnePersonCheckedItemDetailInfo(@RequestBody StatisticsBean obj,@PathVariable Integer page,@PathVariable Integer size){
    	//PageHelper.offsetPage(page, size);
    	PageHelper.startPage(page, size);
    	List<StatisticsBean> returnList=  statisticsService.getOneCheckedDetailInfo(obj);
    	return super.returnSuccessResult(new PageInfo(returnList));
    }
    
    
    /**
     * 获取考核列表情况
     */
    @RequestMapping(value = "/checkman/info")
    public String getCheckmanInfo(@RequestBody CheckmanStatisticsBean obj){
    	CheckmanStatisticsBean returnObj=  statisticsService.getCheckmanStatisticInfo(obj);
    	return super.returnSuccessResult(returnObj);
    }
    
    /**
     * 获取考勤列表情况
     */
    @RequestMapping(value = "/fs/work/info")
    public String getFsworkInfo(@RequestBody StatisticsFsWork obj){
    	List<StatisticsFsWork> returnObj=  statisticsService.getFsWorkStatisticInfo(obj);
    	return super.returnSuccessResultTime(returnObj);
    }
    
    /**
     * 获取顶岗考勤列表情况
     */
    @RequestMapping(value = "/fs/unregular/work/info")
    public String getFsUnregularworkInfo(@RequestBody StatisticsFsWork obj){
    	List<StatisticsFsWork> returnObj=  statisticsService.getUnregularFsWorkStatisticInfo(obj);
    	return super.returnSuccessResultTime(returnObj);
    }
    
    /**
     * 正常在岗人数
     */
    @RequestMapping(value = "/fs/work/person")
    public String getFsworkInfoPersonNumber(@RequestBody StatisticsFsWork obj){
    	List<StatisticsFsWork> returnObj=  statisticsService.getFsWorkPerson(obj);
    	return super.returnSuccessResultTime(returnObj);
    }
    
    /**
     * 顶岗人数统计
     */
    @RequestMapping(value = "/fs/unregular/work/person")
    public String getFsUnregularworkInfoPersonNumber(@RequestBody StatisticsFsWork obj){
    	List<StatisticsFsWork> returnObj=  statisticsService.getFsWorkUnregularWorkPerson(obj);
    	return super.returnSuccessResultTime(returnObj);
    }
    
    /**
     * 申诉统计
     */
    @RequestMapping(value = "/check/appeal")
    public String getCheckAppeal(@RequestBody CheckAppealStatistic obj){
    	List<CheckAppealStatistic> returnObj=  statisticsService.getCheckAppealStatistic(obj);
    	return super.returnSuccessResultTime(returnObj);
    }
    
    /**
     * 业务提升情况反馈表（所有员工排名）
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/business/feedback")
    public String getBusinessFeedback(@RequestBody StatisticsBean obj){
    	List<StatisticsBo> lists=  statisticsService.getBusinessFeedbackInfo(obj);
    	return super.returnSuccessResult(lists);
    }
    
    
    @RequestMapping(value = "/check/wrecker")
    public String getCheckWrecker(@RequestBody StatisticsBean obj){
    	List<StatisticsBean> lists=  statisticsService.getWreckerCheckInfo(obj);
    	return super.returnSuccessResult(lists);
    }
    
    @RequestMapping(value = "/check/wrecker/staff")
    public String getCheckWreckerStaff(@RequestBody WreckerStatisticsBO obj){
    	List<WreckerStatisticsBO> lists=  statisticsService.getWreckerStaffInfo(obj);
    	return super.returnSuccessResult(lists);
    }


    @RequestMapping(value = "/user/score")
    public String getFsOnePersonCheckedItemDetailInfo(@RequestBody UserStatistic obj){
        List<StatisiticPojo> returnList=  statisticsService.getuserStatistic(obj);
        return super.returnSuccessResult((returnList));
    }


	@RequestMapping(value = "/userex/add")
	public String addcheckSExUser(@RequestBody StatisticExUser obj){
    	Object s = statisticsService.addcheckSExUser(obj);
    	if(s==null) throw new BusinessException(20001,"发生错误");
		return super.returnSuccessResult();
	}

	@RequestMapping(value = "/userex/delete")
	public String deletecheckSExUser(@RequestBody StatisticExUser obj){
		Object s=statisticsService.deletecheckSExUser(obj);
		if(s==null) throw new BusinessException(20001,"发生错误");
		return super.returnSuccessResult();
	}

	@RequestMapping(value = "/dept")
	public String getDeptStatistic(@RequestBody DeptStatistic obj){
		Map<String,List<DeptStatistic>> s=statisticsService.getDeptStatistic(obj);
		if(s==null) throw new BusinessException(20002,"参数不对");
		return super.returnSuccessResult(s);
	}

    
}
