package cn.com.countrygarden.bi.main.controller.bigholderSatisfaction;

import cn.com.countrygarden.bi.main.dao.sys.RoleMapper;
import cn.com.countrygarden.bi.main.model.sys.Role;
import cn.com.countrygarden.bi.main.model.twerp.hm.TbSysUser;
import cn.com.countrygarden.bi.main.service.bigholderAduit.BigholderAduitService;
import cn.com.countrygarden.bi.main.service.bigholderSatisfy.BigholderSatisfyService1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.xml.rpc.ServiceException;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/bigholder/satisfaction")
public class BigholderSatisfactionController {

	@Autowired
	BigholderAduitService bigholderAduitService;

	@Autowired
	BigholderSatisfyService1 bigholderSatisfyService1;

	@Autowired
	RoleMapper roleMapper;

	/**
	 * 大业主总部负责人
	 */
	public static final String BIGHEADQUATERS = "dyz_admin";

	/**
	 * 大业主区域负责人
	 */
	public static final String BIGHEADAREA = "dyz_ordinary";

	/**
	 * 公共方法，对区域进行值处理
	 * 
	 * @param area
	 * @return
	 */
	public String changeArea(String area) {
		if (area != null) {
			area = area.length() == 0 ? null : area;
			if (area != null && area.equals("全部区域")) {
				area = null;
			}
		}
		return area;
	}

	public String changeProject(String projectName) {
		if (projectName != null) {
			projectName = projectName.length() == 0 ? null : projectName;
			if (projectName != null && projectName.equals("全部项目")) {
				projectName = null;
			}
		}
		return projectName;
	}

	/**
	 * 分页的公共方法
	 */
	public static Map<String, Object> commPage(List<Map<String, Object>> data, Integer page, Integer pageSize) {
		BigDecimal pages=new BigDecimal(0);
		if(pageSize!=0){
			pages = new BigDecimal(Long.toString(data.size())).divide(new BigDecimal(Long.toString(pageSize)), 0,
				RoundingMode.UP);
		}
		
		BigDecimal currentPage = new BigDecimal(Long.toString(page));
		boolean endOfPage = false;
		if (pages.compareTo(currentPage) <= 0) {
			currentPage=pages;
			endOfPage = true;
		} else {
			endOfPage = false;
		}
		if(currentPage.compareTo(new BigDecimal(0))==0){
			currentPage=new BigDecimal(1);
		}
			
		List<Map<String, Object>> data1 = new ArrayList<>();
		if (data.size() > 0 && data.size() <= pageSize) {
			data1 = data;
		} else if (data.size() > pageSize && data.size() <= currentPage.intValue() * pageSize) {
			for (int i = (currentPage.intValue() - 1) * pageSize; i < data.size(); i++) {
				data1.add(data.get(i));
			}
		} else if (data.size() > currentPage.intValue() * pageSize) {
			for (int i = (currentPage.intValue() - 1) * pageSize; i < currentPage.intValue() * pageSize; i++) {
				data1.add(data.get(i));
			}
		}
		Map<String, Object> map1 = new HashMap<>();
		map1.put("pages", pages);
		map1.put("currentPage", currentPage);
		map1.put("pageSize", pageSize);
		map1.put("endOfPage", endOfPage);
		map1.put("data", data1);
		return map1;
	}

	/**
	 * 全国区域(总部或区域)
	 * @param request
	 * @param yearMon
	 * @param accessToken
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 * @throws ServiceException
	 */
	@RequestMapping(value = "/areaSatisfaction", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object allAreaSatisfaction(HttpServletRequest request,
			@RequestParam(value = "startMonth", defaultValue = "0") String startMonth,
			@RequestParam(value = "endMonth", defaultValue = "0") String endMonth,
			@RequestParam(value = "yearMonth", defaultValue = "0") String yearMonth,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters,
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize)
			throws IOException, ParseException, ServiceException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		List<Role> roles = roleMapper.findByUserCode(tbSysUser.getUserCode());
		Map<String, Object> resultMap = new HashMap<>();

		if (startMonth.length() == 0 && endMonth.length() == 0) {
			startMonth = (LocalDate.now() + "").substring(0, 10);
			endMonth = (LocalDate.now() + "").substring(0, 10);
		}
		Map<String, Object> map1 = new HashMap<>();
		area = this.changeArea(area);
		List<Map<String, Object>> data = bigholderSatisfyService1.getAreaAnalysis(tbSysUser.getUserCode(), area,
				annualState, howQuarters, true, startMonth, endMonth,yearMonth);
		map1 = commPage(data, page, pageSize);
		resultMap.put("resultMsg", "OK");
		resultMap.put("resultCode", 0);
		resultMap.put("data", map1);
		return resultMap;
	}

	/**
	 * 全国区域(总部或区域(满意度和满意率))
	 * @param request
	 * @param yearMon
	 * @param accessToken
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value = "/areaStOrRateInfo", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object areaStOrRateInfo(HttpServletRequest request,
			@RequestParam(value = "startMonth", defaultValue = "0") String startMonth,
			@RequestParam(value = "endMonth", defaultValue = "0") String endMonth,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters)
			throws IOException, ParseException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		List<Role> roles = roleMapper.findByUserCode(tbSysUser.getUserCode());
		Map<String, Object> resultMap = new HashMap<>();
		area = this.changeArea(area);
		List<Map<String, Object>> data = bigholderSatisfyService1.getAreaAnalysis1(tbSysUser.getUserCode(), area, annualState,
						howQuarters, false, startMonth, endMonth);
//		Map<String,Object> map1=this.buttunRight(tbSysUser.getUserCode());
//		data.add(map1);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}

	/**
	 * 导入总裁问卷按钮权限
	 * @param request
	 * @param yearMon
	 * @param accessToken
	 * @return
	 * @throws IOException
	 * @throws ParseException
	 */
	@RequestMapping(value = "/areaButtunRight", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object areaButtunRight(HttpServletRequest request,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken)
			throws IOException, ParseException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = new ArrayList<>();
		Map<String,Object> map1=this.buttunRight(tbSysUser.getUserCode());
		data.add(map1);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}
	
	/**
	 * 查看总裁问卷(点击区域评分)
	 * 
	 * @param request
	 * @param yearMonth
	 * @param area
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/viewPresidentQs", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object viewPresidentQs(HttpServletRequest request,
			@RequestParam(value = "yearMonth", defaultValue = "") String yearMonth,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters,
			@RequestParam(value = "area", defaultValue = "") String area) throws IOException {
		area = this.changeArea(area);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = bigholderSatisfyService1.getViewPresidentQs(yearMonth, area, annualState,
				howQuarters);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}
	
	public Map<String,Object> buttunRight(String userCode){
		Map<String,Object> map=new HashMap<>();
		List<Role> roles = roleMapper.findByUserCode(userCode);
		for (Role role : roles) {
			// 判断是否是总部
			if (role.getCode().equals(BIGHEADQUATERS)) {
				map.put("buttunRight", "0");
//				return map;
				// 非总部管理员
			} else if (role.getCode().equals(BIGHEADAREA)) {
				Map<String,Object> map1=new HashMap<>();
				for (Role role1 : roles) {
					if(role1.getCode().equals(BIGHEADQUATERS)){
						map1.put("buttunRight", "0");
						return map1;
					}
				}
				map.put("buttunRight", "1");
//				return map;
			}
		}
		return map;
	}

	/**
	 * 区域或全国查看详情
	 * 
	 * @param request
	 * @param yearMonth
	 * @param accessToken
	 * @param area
	 * @param annualState判断是年度，月度，季度
	 * @param howQuarters判断月度是第几个月度
	 * @param regionalScope
	 *            0为全国和区域，1为项目
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/viewDetails", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object viewDetails(HttpServletRequest request,
			@RequestParam(value = "startMonth", defaultValue = "0") String startMonth,
			@RequestParam(value = "endMonth", defaultValue = "0") String endMonth,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "regionalScope", defaultValue = "0") String regionalScope,
			@RequestParam(value = "projectName", defaultValue = "") String projectName,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters) throws IOException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		area = this.changeArea(area);
		projectName = this.changeProject(projectName);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = new ArrayList<>();
		if ("0".equals(regionalScope)) {
			data = bigholderSatisfyService1.getAreaViewPresidentQs(area, tbSysUser.getUserCode(), annualState,
					howQuarters, startMonth, endMonth);
		} else if ("1".equals(regionalScope)) {
			data = bigholderSatisfyService1.getProjectViewPresidentQs(area, tbSysUser.getUserCode(), projectName,
					annualState, howQuarters, startMonth, endMonth);
		}
		data.add(this.buttunRight(tbSysUser.getUserCode()));
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}
	
	/**
	 * 区域或全国查看详情统计得分
	 * @param request
	 * @param yearMonth
	 * @param accessToken
	 * @param area
	 * @param annualState判断是年度，月度，季度
	 * @param howQuarters判断月度是第几个月度
	 * @param regionalScope
	 *            0为全国和区域，1为项目
	 * @param page
	 * @param pageSize
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/statisticalScores", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object statisticalScores(HttpServletRequest request,
			@RequestParam(value = "startMonth", defaultValue = "0") String startMonth,
			@RequestParam(value = "endMonth", defaultValue = "0") String endMonth,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "regionalScope", defaultValue = "0") String regionalScope,
			@RequestParam(value = "projectName", defaultValue = "") String projectName,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters) throws IOException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		area = this.changeArea(area);
		projectName = this.changeProject(projectName);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = new ArrayList<>();
		if ("0".equals(regionalScope)) {
			data = bigholderSatisfyService1.getAreaStatisticalScores(area, tbSysUser.getUserCode(), annualState,
					howQuarters, startMonth, endMonth);
		} else if ("1".equals(regionalScope)) {
			data = bigholderSatisfyService1.getProjectStatisticalScores(area, tbSysUser.getUserCode(), projectName,
					annualState, howQuarters, startMonth, endMonth);
		}
//		data.add(this.buttunRight(tbSysUser.getUserCode()));
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}

	/**
	 * 主观评价（切换区域或全国）
	 * 
	 * @param request
	 * @param yearMonth
	 * @param accessToken
	 * @param area
	 * @param page
	 * @param pageSize
	 * @param regionalScope
	 *            0为全国和区域，1为项目
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/mainEvaluation", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object mainEvaluation(HttpServletRequest request,
			@RequestParam(value = "startMonth", defaultValue = "0") String startMonth,
			@RequestParam(value = "endMonth", defaultValue = "0") String endMonth,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "annualState", defaultValue = "0") String annualState,
			@RequestParam(value = "howQuarters", defaultValue = "1") String howQuarters,
			@RequestParam(value = "regionalScope", defaultValue = "0") String regionalScope,
			@RequestParam(value = "projectName", defaultValue = "") String projectName,
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer page,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) throws IOException {
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		area = this.changeArea(area);
		projectName = this.changeProject(projectName);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = new ArrayList<>();
		if ("0".equals(regionalScope)) {
			data = bigholderSatisfyService1.mainEvaluation(area, tbSysUser.getUserCode(), annualState, howQuarters,
					startMonth, endMonth);
		} else if ("1".equals(regionalScope)) {
			data = bigholderSatisfyService1.mainEvaluationPro(projectName, tbSysUser.getUserCode(), area, annualState,
					howQuarters, startMonth, endMonth);
		}
		Map<String, Object> map1 = commPage(data, page, pageSize);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", map1);
		return resultMap;
	}

	/**
	 * 获取项目列表
	 * 
	 * @param request
	 * @param yearMonth
	 * @param accessToken
	 * @param projectName
	 * @return
	 */
	@RequestMapping(value = "/getProjectName", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object getProjectName(HttpServletRequest request,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "area", defaultValue = "") String area,
			@RequestParam(value = "projectName", defaultValue = "") String projectName) throws IOException {
		area = this.changeArea(area);
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		projectName = this.changeProject(projectName);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = bigholderSatisfyService1.getProjectName(tbSysUser.getUserCode(), area,
				projectName,tbSysUser);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}

	/**
	 * 获取区域列表
	 * 
	 * @param request
	 * @param yearMonth
	 * @param accessToken
	 * @param projectName
	 * @return
	 */
	@RequestMapping(value = "/getAreas", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	public @ResponseBody Object getAreas(HttpServletRequest request,
			@RequestParam(value = "AccessToken", defaultValue = "") String accessToken,
			@RequestParam(value = "a", defaultValue = "0") String a,
			@RequestParam(value = "area", defaultValue = "") String area) throws IOException {
		area = this.changeArea(area);
		TbSysUser tbSysUser = bigholderAduitService.getTbSysUser(accessToken);
		Map<String, Object> resultMap = new HashMap<>();
		List<Map<String, Object>> data = bigholderSatisfyService1.getAreas(area, tbSysUser.getUserCode(),a);
		resultMap.put("resultCode", 0);
		resultMap.put("resultMsg", "OK");
		resultMap.put("data", data);
		return resultMap;
	}
}