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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.yonyou.hrcloud.pm.ps.service.IPMProgressService;
import io.swagger.annotations.*;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import springfox.documentation.annotations.ApiIgnore;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StopWatch;
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.alibaba.fastjson.JSONObject;
import com.yonyou.hrcloud.adder.context.AppContext;
import com.yonyou.hrcloud.adder.exception.AdderBusinessException;
import com.yonyou.hrcloud.adder.service.IGenericService;
import com.yonyou.hrcloud.berry.model.MessageResult;
import com.yonyou.hrcloud.pm.common.model.CommonValue;
import com.yonyou.hrcloud.pm.common.model.OpLog;
import com.yonyou.hrcloud.pm.common.model.PmStaff;
import com.yonyou.hrcloud.pm.common.web.BaseController;
import com.yonyou.hrcloud.pm.helper.Toolkit;
import com.yonyou.hrcloud.pm.ps.model.EvaObject;
import com.yonyou.hrcloud.pm.ps.model.Scheme;
import com.yonyou.hrcloud.pm.ps.service.IOperationPartialService;
import com.yonyou.hrcloud.pm.ps.service.IOperationService;
import com.yonyou.hrcloud.pm.ps.service.ISchemeService;
import com.yonyou.hrcloud.pm.ps.service.ISheetService;

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

/**
 * 方案流程控制器
 * 
 * @author Flysea
 *
 */
@RestController
@RequestMapping("/pm/flow")
@Api(value = "OperationSchemeController | 方案流程控制器")
public class OperationSchemeController extends BaseController {

	@Autowired
	ISchemeService schemeService;

	@Autowired
	ISheetService sheetService;

	@Autowired
	IOperationService opService;
	
	@Autowired
	IOperationPartialService op2Service;

	@Autowired
	IPMProgressService pmprogressService;
    @Autowired
    IGenericService daoex;
    
	/**
	 * 删除方案
	 * 
	 * @param data
	 *            等级id
	 * @return
	 */
	@RequestMapping(value = "/delScheme", method = RequestMethod.POST)
	@ApiOperation(value = "删除方案")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:考核方案id}", required = true, dataType = "Map")
	@ApiIgnore
	public MessageResult delScheme(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("删除成功！",null,"pm.i18n_pbc.comm.002");
		String id = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(id, false);
		super.IsExist(scheme);
		if (scheme.getSchemestate() != CommonValue.EnumSchemeState.State2.ordinal())
			throw new AdderBusinessException("方案状态不正确","pm.i18n_pbc.ps.003");
		opService.delete(id);
		return result;
	}

	/**
	 * 复制方案
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/copyScheme", method = RequestMethod.POST)
	@ApiOperation(value = "复制方案")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id',newname:'考核方案名称',state:'完成状态'}", required = true, dataType = "Map")
	public MessageResult copyScheme(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		String schemename = data.get("newname");
		String state = data.get("state");
		int iState = 1;
		if (state != null)
			iState = Integer.valueOf(state);
		String newid = op2Service.copyScheme(schemeid, schemename, iState);
		result.setData(newid);
		return result;
	}

	/**
	 * 创建中期回顾方案
	 *
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/savemtrScheme", method = RequestMethod.POST)
	@ApiOperation(value = "创建中期回顾方案")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id',newname:'考核方案名称'}", required = true, dataType = "Map")
	public MessageResult savemtrScheme(@RequestBody String data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		if (data == null || data.length() == 0) {
			throw new AdderBusinessException("参数不能为空", "pm.i18n_core.comm.002");
		}
		JSONObject json = JSONObject.parseObject(StringEscapeUtils.unescapeHtml(data));
		Scheme newScheme = op2Service.savemtrScheme(json);
		result.setData(newScheme);
		return result;
	}

	/**
	 * 启动方案
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/submitScheme", method = RequestMethod.POST)
	@ApiOperation(value = "启动方案")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id'}", required = true, dataType = "Map")
	public MessageResult submitScheme(@RequestBody Map<String, Object> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String id = data.get("id").toString();
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(id, true);
		super.IsExist(scheme);
		if (scheme.getSchemestate() != CommonValue.EnumSchemeState.State2.ordinal())
			throw new AdderBusinessException("方案状态不正确", "pm.i18n_pbc.ps.003");
		if(scheme.getSchememode() == CommonValue.EnumSchemeMode.pm_mtr.getIndex()){
			String isdel = data.get("isdel") == null ? "" : data.get("isdel").toString();
			if(StringUtils.isNotEmpty(isdel) && ! "1".equals(isdel)){
				throw new AdderBusinessException("不正确的参数", "pm.i18n_pbc.pa.001");
			}
			List<String> staffids = new ArrayList<>();
			if(data.get("staffids") != null){
				staffids = (List<String>)data.get("staffids");
			}
			result.setData(op2Service.submitMtr(scheme,isdel,staffids));
		}else{
			opService.submit(scheme);
		}
		return result;
	}
	
	/**
	 * 启动考核监控  ##方案启动完后，启动新加的考核对象-（生成量表等操作）。
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/submitSchemeMonitor", method = RequestMethod.POST)
	@ApiOperation(value = "启动考核监控")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{schemeid:'考核方案id',staffid:[]}", required = true, dataType = "Map")
	public MessageResult submitSchemeMonitor(@RequestBody JSONObject data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.getString("schemeid");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, true);
		super.IsExist(scheme);
		if(scheme.getSchemestate() == CommonValue.EnumSchemeState.State2.getIndex() 
				|| scheme.getSchemestate() >= CommonValue.EnumSchemeState.State6.getIndex()){
			throw new AdderBusinessException("方案状态不正确", "pm.i18n_pbc.ps.003");
		}
		JSONArray jsonArray = data.getJSONArray("evaobject");
		if(jsonArray == null ||jsonArray.size() <= 0){
			throw new AdderBusinessException("参数不能为空", "pm.i18n_core.comm.002");
		}
		List<EvaObject> objs = new ArrayList<EvaObject>();
        for (int i = 0 ; i < jsonArray.size() ; i++) {
            // JSONObject resultJson = JSONObject.parseObject(json.toString());
			JSONObject resultJson = jsonArray.getJSONObject(i); // 解决json升级导致的问题
            EvaObject newObj = new EvaObject();
            if(StringUtils.isEmpty(resultJson.getString("groupid")) 
            		|| StringUtils.isEmpty(resultJson.getString("staffid"))){
            	throw new AdderBusinessException("参数不能为空", "pm.i18n_core.comm.002");
            }
            newObj.setGroupid(resultJson.getString("groupid"));
            newObj.setGaugeid(resultJson.getString("gaugeid"));
            newObj.setSchemeid(schemeid);
            newObj.setStaffid(resultJson.getString("staffid"));
            List approver = resultJson.getObject("approver", List.class); // List<String>
            List appraiser = resultJson.getObject("appraiser", List.class); //List<Map>
            newObj.setAppraiserList(appraiser);
            newObj.setApproverList(approver);
            objs.add(newObj);
        }
		// 创建考核对象，保存考核人与审核人，启动方案，创建量表，创建指标，审核人，考核人
        opService.submitSchemeMonitor(scheme, objs);
		return result;
	}

	/**
	 * 重新设置
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/cancelScheme", method = RequestMethod.POST)
	@ApiOperation(value = "重新设置")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id'}", required = true, dataType = "Map")
	public MessageResult cancelScheme(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("重新设置成功",null,"pm.i18n_pbc.comm.005");
		String id = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(id, true);
		super.IsExist(scheme);
		if (scheme.getSchemestate() >= CommonValue.EnumSchemeState.State4.ordinal())
			throw new AdderBusinessException("方案填报已结束","pm.i18n_pbc.ps.003");
		opService.cancelSubmit(scheme);
		return result;
	}

	/**
	 * 开始填报
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/completing", method = RequestMethod.POST)
	@ApiOperation(value = "开始填报")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id'}", required = true, dataType = "Map")
	public MessageResult completing(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if(scheme.getSchememode() == CommonValue.EnumSchemeMode.pm_mtr.getIndex()){
			op2Service.completingMtr(scheme);
		}else{
			opService.completing(scheme);
		}
		return result;
	}

	/**
	 * 填报结束
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/completed", method = RequestMethod.POST)
	@ApiOperation(value = "填报结束")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map:{id:'考核方案id',isforce:'是否有效'}", required = true, dataType = "Map")
	public MessageResult completed(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		boolean isForce = Boolean.valueOf(data.get("isforce"));
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if(scheme.getSchememode() == CommonValue.EnumSchemeMode.pm_mtr.getIndex() && ! scheme.isIsallowcalc()){
			List<PmStaff> objs = op2Service.completedMtr(scheme, isForce);
			result.setData(objs);
		}else{
			List<PmStaff> objs = opService.completed(scheme, isForce);
			result.setData(objs);
		}
		return result;
	}

	/**
	 * 重新填报
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/refill", method = RequestMethod.POST)
	@ApiOperation(value = "重新填报")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:'考核方案id',data:'[staffid1,staffid2,...]'}", required = true, dataType = "Map")
	public MessageResult refill(@RequestBody Map<String, String> data) {
		if (data.get("id") == null || data.get("data") == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		List<String> staffids = JSONArray.parseArray(data.get("data"),
				String.class);
		opService.refill(scheme, staffids);
		return result;
	}

	/**
	 * 开始评分
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/startScore", method = RequestMethod.POST)
	@ApiOperation(value = "开始评分")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult startScore(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		opService.startScore(scheme);
		return result;
	}

	/**
	 * 评分结束
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/calculating", method = RequestMethod.POST)
	@ApiOperation(value = "评分结束")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id,isforce:'是否有效'}", required = true, dataType = "Map")
	public MessageResult calculating(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		boolean isForce = Boolean.valueOf(data.get("isforce"));
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if(scheme.getSchememode() == CommonValue.EnumSchemeMode.pm_mtr.getIndex()){
			List<PmStaff> objs = op2Service.calculatingMtr(scheme, isForce);
			result.setData(objs);
		}else{
			List<PmStaff> objs = opService.calculating(scheme, isForce);
			result.setData(objs);
		}
		return result;
	}

	/**
	 * 重新评分
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/rescore", method = RequestMethod.POST)
	@ApiOperation(value = "重新评分")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:'考核方案id',data:'[staffid1,staffid2,...]'}", required = true, dataType = "Map")
	public MessageResult rescore(@RequestBody Map<String, Object> data) {
		if (data.get("id") == null || data.get("data") == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id").toString();
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		List<String> staffids = JSONArray.parseArray(data.get("data")
				.toString(), String.class);
		opService.rescore(scheme, staffids);
		return result;
	}

	/**
	 * 开始结果审核
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/initAudit", method = RequestMethod.POST)
	@ApiOperation(value = "开始结果审核")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult initAudit(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		// 校验方案，initAudit需附带考核对象组信息
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, true, false);
		super.IsExist(scheme);
		// 触发开始结果审核
		opService.initAudit(scheme);
		return result;
	}

	/**
	 * 结果审核结束
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/endAudit", method = RequestMethod.POST)
	@ApiOperation(value = "结果审核结束")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult endAudit(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		opService.endAudit(scheme);
		return result;
	}

	/**
	 * 重新结果审核
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/reexamined", method = RequestMethod.POST)
	@ApiOperation(value = "重新结果审核")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id,data:[groupid1,groupid2,...]}", required = true, dataType = "Map")
	public MessageResult reexamined(@RequestBody Map<String, String> data) {
		if (data.get("id") == null || data.get("data") == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		List<String> groupids = JSONArray.parseArray(data.get("data"),
				String.class);
		opService.reexamined(scheme, groupids);
		return result;
	}

	/**
	 * 取消结果审核
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/cancelAudit", method = RequestMethod.POST)
	@ApiOperation(value = "取消结果审核")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult cancelAudit(@RequestBody Map<String, String> data) {
		if (data.get("id") == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		// 方案状态回退到评分已结束，可点击重新评分、开始审核
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		opService.cancelAudit(scheme);
		return result;
	}
	
	/**
	 * 结果发布
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/publish", method = RequestMethod.POST)
	@ApiOperation(value = "结果发布")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult publish(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		opService.publish(scheme);
		return result;
	}

	/**
	 * 取消发布
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/cancelPublic", method = RequestMethod.POST)
	@ApiOperation(value = "取消发布")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult cancelPublic(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if (scheme.getSchemestate() != CommonValue.EnumSchemeState.State9.getIndex())
			throw new AdderBusinessException("方案状态不正确","pm.i18n_pbc.ps.003");
		opService.cancelPublic(scheme);
		return result;
	}

	/**
	 * 查询方案下考核对象集合，为重填、重评分等
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/queryStaffForRefill", method = RequestMethod.POST)
	@ApiOperation(value = "查询方案下考核对象集合，为重填、重评分等")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult queryStaffForRefill(@RequestBody Map<String, String> data) {
		StopWatch stopWatch = new StopWatch("pm:queryStaffForRefill");
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		stopWatch.start("queryAuthorizedSchemeByID " + Toolkit.getNow());
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		stopWatch.stop();
		super.IsExist(scheme);
		if (scheme.getSchemestate() < CommonValue.EnumSchemeState.State4.getIndex())
			throw new AdderBusinessException("方案状态不正确","pm.i18n_pbc.ps.003");
		stopWatch.start("queryStaffByScheme " + Toolkit.getNow());
		result.setData(sheetService.queryStaffByScheme(schemeid));
		stopWatch.stop();
		if (stopWatch.getTotalTimeSeconds() > 1) {
			OpLog log = new OpLog();
			log.setLogtype(10);
			log.setName("效率");
			log.setContent("queryStaffForRefill");
			log.setDefined1(stopWatch.prettyPrint());
			log.setLogdate(new Date());
			WriteLog(log);
		}
		return result;
	}

	/**
	 * 查询方案需提醒人员
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/queryStaffForRemind", method = RequestMethod.POST)
	@ApiOperation(value = "查询方案需提醒人员")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id}", required = true, dataType = "Map")
	public MessageResult queryStaffForRemind(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		if (schemeid == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		CommonValue.EnumWorkStep step = null;
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State3.getIndex())
			step = CommonValue.EnumWorkStep.step1;
		else if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State5.getIndex())
			step = CommonValue.EnumWorkStep.step2;
		else if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State7.getIndex())
			step = CommonValue.EnumWorkStep.step3;
		else
			return result;
		Map<String, Object> objs = opService.queryStaffByConditon(schemeid,
				step);
		result.setData(objs);
		return result;
	}

	/**
	 * 查询提醒明细
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/queryRemindDetail", method = RequestMethod.POST)
	@ApiOperation(value = "查询提醒明细")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map参数:{id:考核方案id，staffid:人员id}", required = true, dataType = "Map")
	public MessageResult queryRemindDetail(@RequestBody Map<String, String> data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		String schemeid = data.get("id");
		String staffid = data.get("staffid");
		if (schemeid == null || staffid == null)
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		CommonValue.EnumWorkStep step = null;
		Scheme scheme = schemeService.queryAuthorizedSchemeByID(schemeid, false);
		super.IsExist(scheme);
		if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State3.getIndex())
			step = CommonValue.EnumWorkStep.step1;
		else if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State5.getIndex())
			step = CommonValue.EnumWorkStep.step2;
		else if (scheme.getSchemestate() == CommonValue.EnumSchemeState.State7.getIndex())
			step = CommonValue.EnumWorkStep.step3;
		else
			return result;
		result.setData(opService.queryRemindDetail(schemeid, step, staffid));
		return result;
	}

	/**
	 * 获取方案的考核对象组
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/queryEvaGroupByScheme", method = RequestMethod.GET)
	@ApiOperation(value = "获取方案的考核对象组")
	@ApiImplicitParam(paramType = "query", name = "id", value = "考核方案id", required = true, dataType = "String")
	public MessageResult queryEvaGroupByScheme(String id) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		Scheme obj = schemeService.querySchemeByID(id, true, false);
		result.setData(obj.getOGroups());
		return result;
	}

	/**
	 * 发送提醒
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
	@ApiOperation(value = "发送提醒")
	@ApiImplicitParam(paramType = "body", name = "data", value = "json参数:{schemeid:考核方案id,staff:'[staffid1,staffid2,...]',sendtype:'发送状态码'}", required = true, dataType = "String")
	public MessageResult sendMessage(@RequestBody String data) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		JSONObject json = JSONObject.parseObject(data);
		String schemeid = json.getString("schemeid");
		List<String> staffids = JSONArray.parseArray(json.getString("staff"),
				String.class);
		Integer sendType = json.getInteger("sendtype");
		String ret = opService.sendReminder(schemeid, staffids, sendType, null);
		if (ret.length() > 0)
			throw new AdderBusinessException("{0}", "pm.i18n_flow.wf.001", ret);
		return result;
	}

	@RequestMapping(value = "/querySchemeRateByPeriodCode", method = RequestMethod.POST)
	@ApiOperation(value = "根据绩效期间code查询填报进展", httpMethod = "POST")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map{'periodcode':'绩效周期'}", required = true)
	public MessageResult querySchemeRateByPeriodCode(@RequestBody String data) {
		MessageResult result = new MessageResult("操作成功", null, "pm.i18n_core.comm.008");
		JSONObject obj = JSON.parseObject(StringEscapeUtils
				.unescapeHtml(data));
		if (obj.size() > 0 &&  StringUtils.isNotEmpty(obj.getString("periodcode"))) {
			int pageno = obj.getIntValue("pageno");
			int pagesize = obj.getIntValue("pagesize") == 0 ?10:obj.getIntValue("pagesize");
			result.setData(opService.querySchemeRateByPeriodCode(obj.getString("periodcode"),pageno,pagesize));
		} else {
			throw new AdderBusinessException("请传入绩效期间", "pm.i18n_core.ps.009");
		}
		return result;
	}

	@RequestMapping(value = "/exportEvaObjectByPeriodCode", method = RequestMethod.GET)
	@ApiOperation(value = "根据绩效期间code导出填报进展", httpMethod = "GET")
	@ApiImplicitParam(name = "periodcode", value = "绩效期间code", dataType = "String", paramType = "query", required = true)
	public MessageResult exportEvaObjectByPeriodCode(HttpServletRequest request, HttpServletResponse
			response, String periodcode) {
		if (StringUtils.isNotEmpty(periodcode)) {
			opService.exportEvaObjectByPeriodCode(request,response,periodcode);
		} else {
			throw new AdderBusinessException("请传入绩效期间", "pm.i18n_core.ps.009");
		}
		return null;
	}

	@RequestMapping(value = "/exportEvaObjectBySchemeId", method = RequestMethod.GET)
	@ApiOperation(value = "根据该方案id导出方案下考核对象的考核结果", httpMethod = "GET")
	@ApiImplicitParam(name = "schemeid", value = "方案id", dataType = "String", paramType = "query", required = true)
	public MessageResult exportEvaObjectBySchemeId(HttpServletRequest request, HttpServletResponse
			response, String schemeid) {
		if (StringUtils.isNotEmpty(schemeid)) {
			Scheme scheme = daoex.load(Scheme.class, schemeid);
			if (scheme == null)
				throw new AdderBusinessException("方案不存在", "pm.i18n_core.ps.043");
			if(scheme.getSchemestate() < CommonValue.EnumSchemeState.State8.getIndex()){
				throw new AdderBusinessException("方案状态不正确","pm.i18n_pbc.ps.003");
			}
			opService.exportEvaObjectBySchemeId(request,response,scheme);
		} else {
			throw new AdderBusinessException("不正确的参数", "pm.i18n_pbc.pa.001");
		}
		return null;
	}
	
	@RequestMapping(value = "/queryMonitorInfoByScheme", method = RequestMethod.POST)
	@ApiOperation(value = "根据方案id查询方案的考核监控信息", httpMethod = "POST")
	@ApiImplicitParam(paramType = "body", name = "data", value = "map{'schemeid':'方案id查'}", required = true)
	public MessageResult queryMonitorInfoByScheme(@RequestBody String data) {
		MessageResult result = new MessageResult("操作成功", null, "pm.i18n_core.comm.008");
		JSONObject obj = JSON.parseObject(StringEscapeUtils
				.unescapeHtml(data));
		if (obj.size() > 0 &&  StringUtils.isNotEmpty(obj.getString("schemeid"))) {
			String schemeid = obj.getString("schemeid");
			Scheme scheme = daoex.load(Scheme.class, schemeid);
			if(scheme == null){
				throw new AdderBusinessException("方案不存在","pm.i18n_core.ps.043");
			}
			if(scheme.getSchemestate() < CommonValue.EnumSchemeState.State3.getIndex()){
				throw new AdderBusinessException("方案状态不正确","pm.i18n_pbc.ps.003");
			}
			result.setData(pmprogressService.queryMonitorInfoByScheme(scheme));
		} else {
			throw new AdderBusinessException("不正确的参数","pm.i18n_pbc.pa.001");
		}
		return result;
	}
	
	/**
	 * 批量完全删除考核对象
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/deleteObjects", method = RequestMethod.POST)
	@ApiOperation(value = "完全删除考核对象", httpMethod = "POST")
	public MessageResult deleteObjects(@RequestBody Map<String, Object> data) {
		MessageResult result = new MessageResult("操作成功", null, "pm.i18n_core.comm.008");
		String schemeid = (String) data.get("schemeid");
		List<String> staffids = (List<String>) data.get("staffids");
		if (data == null || StringUtils.isEmpty(schemeid) || CollectionUtils.isEmpty(staffids))
			throw new AdderBusinessException("不正确的参数", "pm.i18n_pbc.pa.001");
		Scheme scheme = daoex.load(Scheme.class, schemeid);
		if (scheme == null)
			throw new AdderBusinessException("方案不存在", "pm.i18n_core.ps.043");
		// 权限校验
		if (!scheme.getCreator().equals(AppContext.getContext().getUserId()))
			throw new AdderBusinessException("权限受限，请联系管理人员", "pm.i18n_core.system.001");
		for (String staffid : staffids) {
			pmprogressService.deleteObject(schemeid, staffid);
		}
		return result;
	}

	@RequestMapping(value = "/exportFinalScoreAndIndiScore", method = RequestMethod.GET)
	@ApiOperation(value = "导出绩效考核结果与指标得分", httpMethod = "GET")
	@ApiImplicitParam(name = "schemeid", value = "考核方案id", dataType = "String", paramType = "query", required = true)
	public MessageResult exportFinalScoreAndIndiScore(HttpServletRequest request, HttpServletResponse
			response, String schemeid) {
		if (StringUtils.isNotEmpty(schemeid)) {
			opService.exportFinalScoreAndIndiScore(request,response,schemeid);
		} else {
			throw new AdderBusinessException("请传入考核方案", "pm.i18n_core.ps.009");
		}
		return null;
	}
}
