package com.yonyou.hrcloud.pm.ia.web;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.yonyou.hrcloud.adder.context.AppContext;
import com.yonyou.hrcloud.adder.exception.AdderBusinessException;
import com.yonyou.hrcloud.berry.model.MessageResult;
import com.yonyou.hrcloud.pm.common.web.BaseController;
import com.yonyou.hrcloud.pm.helper.StaffHelper;
import com.yonyou.hrcloud.pm.ia.service.ISheetSettingService;
import com.yonyou.hrcloud.pm.ps.model.Sheet;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 指标填报量表控制器
 * 主要提供考核表提交，收回，考核对象删除，以及个人绩效磁贴等功能
 * @author nijb@yonyou.com
 *
 */
@RestController
@RequestMapping("/pm/systempbc")
@Api(value = "SheetSettingController | 指标填报量表控制器")
public class SheetSettingController extends BaseController {

	@Autowired
	ISheetSettingService sheetSettingService;

	/**
	 * 量表提交
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/submitSheet", method = RequestMethod.POST)
    @ApiOperation(value = "量表提交",httpMethod = "POST")
    @ApiImplicitParam(name = "data", value = "{'sheetid':'量表id'}",paramType = "body",  required = true)
	public MessageResult submitSheet(@RequestBody Map<String, Object> data) {
		if (data == null || data.size() == 0) {
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		String sheetid = (String) data.get("sheetid");
		if(StringUtils.isEmpty(sheetid)){
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		Sheet sheet = sheetSettingService.submitSheet(sheetid); // 提交量表
		MessageResult result = new MessageResult("提交成功",null,"pm.i18n_pbc.comm.001");
		result.setData(sheet);
		return result;
	}

	/**
	 * 撤回量表
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/callbackSheet", method = RequestMethod.POST)
    @ApiOperation(value = "撤回量表",httpMethod = "POST")
    @ApiImplicitParam(name = "data", value = "{'sheetid':'量表id'}",paramType = "body",  required = true)
	public MessageResult callbackSheet(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		if (data == null || data.size() == 0) {
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		String sheetid = (String) data.get("sheetid");
		if(StringUtils.isEmpty(sheetid)){
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		Sheet sheet = sheetSettingService.callbackSheet(sheetid); // 提交量表
		result.setData(sheet);
		return result;
	}

	/**
	 * 删除考核对象组
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/deleteEvaGroup", method = RequestMethod.POST)
    @ApiOperation(value = "删除考核对象组",httpMethod = "POST")
    @ApiImplicitParam(name = "data", value = "{'id':'主键id'}",paramType = "body",  required = true)
	public MessageResult deleteEvaGroup(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("删除成功", null, "pm.i18n_core.comm.013");
		String id = data.get("id");
		if(StringUtils.isEmpty(id)){
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		sheetSettingService.deleteEvaGroup(id);
		return result;
	}

	/**
	 * 删除考核对象（批量）
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/deleteEvaObject", method = RequestMethod.POST)
    @ApiOperation(value = "删除考核对象（批量）",httpMethod = "POST")
    @ApiImplicitParam(name = "data", value = "['主键1','主键2']",paramType = "body",  required = true, dataType="String")
	public MessageResult deleteEvaObject(@RequestBody String data) {
		MessageResult result = new MessageResult("删除成功", null, "pm.i18n_core.comm.013");
		if (data == null || data.trim().length() == 0) {
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		List<String> ids = JSONArray.parseArray(
				StringEscapeUtils.unescapeHtml(data), String.class);
		sheetSettingService.deleteObject(ids);
		return result;
	}
	
	/**
	 * PBC我的工作目标
	 * 
	 * @param cond
	 * @return
	 */
	@RequestMapping(value = "/queryMyWorkGoal", method = RequestMethod.GET)
    @ApiOperation(value = "我的工作目标",httpMethod = "GET")
	public MessageResult queryMyWorkGoal() {
		MessageResult result = MessageResult.SUCCESSMSG;
		String staffid = StaffHelper.getStaffByUserId(AppContext.getContext().getUserId());
		List<Map<String, Object>> objs = sheetSettingService.queryMyWorkGoal(staffid);
		result.setData(objs);
		return result;
	}
	
	/**
	 * PBC一个工作目标
	 * 
	 * @param cond
	 * @return
	 */
	@RequestMapping(value = "/queryOneWorkGoal", method = RequestMethod.GET)
    @ApiOperation(value = "一个工作目标",httpMethod = "GET")
    @ApiImplicitParam(name = "sheetid", value = "量表id",paramType = "query",  required = true, dataType = "String")
	public MessageResult queryOneWorkGoal(String sheetid) {
		MessageResult result = MessageResult.SUCCESSMSG;
		Map<String, Object> objs = sheetSettingService.queryOneWorkGoal(sheetid);
		result.setData(objs);
		return result;
	}
	/**
	 * 我的绩效
	 * 
	 * @param cond
	 * @return
	 */
	@RequestMapping(value = "/queryMyPerfList", method = RequestMethod.GET)
    @ApiOperation(value = "我的绩效",httpMethod = "GET")
	public MessageResult queryMyPerfList() {
		MessageResult result = MessageResult.SUCCESSMSG;
		String userId = AppContext.getContext().getUserId();
		String staffid = StaffHelper.getStaffByUserId(userId);
		List<Map<String, Object>> objs = sheetSettingService.queryMyPerfList(staffid);
		result.setData(objs);
		return result;
	}
	/**
	 * 我的绩效-个人绩效
	 * 
	 * @param cond
	 * @return
	 */
	@RequestMapping(value = "/queryAchievement", method = RequestMethod.GET)
    @ApiOperation(value = "我的绩效-个人绩效",httpMethod = "GET")
	public MessageResult queryAchievement() {
		//InvocationInfoProxy.setUserid("f4a08a4a-1a5e-4dbe-ab85-7b5d976a760e");
		//InvocationInfoProxy.setTenantid("iqopszg1"); 
		MessageResult result = MessageResult.SUCCESSMSG;
		String userId = AppContext.getContext().getUserId();
		String staffid = StaffHelper.getStaffByUserId(userId);
		List<Map<String, Object>> objs = sheetSettingService.queryAchievement(staffid);
		result.setData(objs);
		return result;
	}
	/**
	 * 我的绩效：待处理方案数量
	 * @param cond
	 * @return
	 */
	@RequestMapping(value = "/queryMyPerfTreatedCnt", method = RequestMethod.GET)
    @ApiOperation(value = "我的绩效：待处理方案数量",httpMethod = "GET")
	public MessageResult queryMyPerfTreatedCnt(HttpServletResponse response, HttpServletRequest request) {
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
		response.setHeader("Access-Control-Allow-Credentials","true");
    	response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,PATCH,OPTIONS");
    	response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, Authorization");
		MessageResult result = MessageResult.SUCCESSMSG;
		//测试代码
		//InvocationInfoProxy.setUserid("cf2b47a7-2f07-4741-9057-856ceed84a5a");
		//InvocationInfoProxy.setTenantid("iqopszg1");  //String staffid = "0c4569d6e3674208b8262677015c7d7d";
		
		String staffid = StaffHelper.getStaffByUserId( AppContext.getContext().getUserId());
		//List<Map<String, Object>> objs = sheetSettingService.queryMyPerfList(staffid);
		List<Map<String,Object>> objs = sheetSettingService.queryAchievement(staffid);
		Integer total_cnt = 0;
		for(Map<String, Object>  schemeObj : objs){
			Integer tag_code = (Integer)schemeObj.get("tag_code");
			if(tag_code == 1 || tag_code == 4 ){//统计填报和自评的数量
				total_cnt ++ ;
			}
		}
		Map<String,Object> rtn_data = new HashMap<String,Object>();
		rtn_data.put("scheme_treated_cnt", total_cnt);
		result.setData(rtn_data);
		return result;
	}
}
