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

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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.pa.model.Appraise;
import com.yonyou.hrcloud.pm.pa.service.IAppraiseService;
import com.yonyou.hrcloud.pm.result.model.Score;
import com.yonyou.hrcloud.pm.result.service.IApproveScoreService;

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

/**
 * Web端结果审核控制器
 * 
 * @author nijb@yonyou.com
 *
 */
@RestController
@RequestMapping("/pm/result")
@Api(value = "AuditScoreController | Web端结果审核控制器")
public class AuditScoreController extends BaseController {

	@Autowired
	public IApproveScoreService appScoreService;

    @Autowired
    IAppraiseService apprService;

	/**
	 * web端--查询我的待处理方案
	 * 
	 * @param flag
	 *            0未审核；1已审核
	 * @return MessageResult
	 */
	@RequestMapping(value = "/queryMyAuditResultByState", method = RequestMethod.GET)
	@ApiOperation(value = "查询我的待处理方案，支持多方案",httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "flag", value = "状态值(0：未审核/1：已审核)", required = true, dataType = "Integer")
	public MessageResult queryMyAuditResultByState(Integer flag) {
		if (flag == null) {
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		List<Object> objs = appScoreService.queryMultiSchemeByAuditState(flag);
		List<Map> dat = appScoreService.packingMyAuditResult(objs, flag);
		result.setData(dat);
		return result;
	}

	/**
	 * web端结果审核--强制分布情况查询
	 * 
	 * @param id
	 *            考核组id、方案id、多方案组id
	 * @param flag
	 *            0未审核；1已审核
	 * @return MessageResult
	 */
	@RequestMapping(value = "/queryForceDistSituation", method = RequestMethod.GET)
	@ApiOperation(value = "强制分布情况查询",httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "flag", value = "状态值(0：未审核/1：已审核)", required = true, dataType = "Integer")
	public MessageResult queryForceDistSituation(String id, Integer flag, Integer markFlag) {
		if (flag == null) {
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		}
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		List<Object> objs = appScoreService.queryForceDistSituation(id, flag, markFlag);
		result.setData(objs);
		return result;
	}
	/**
	 * web端结果审核-- 查看审核历史
	 * 
	 * @param id
	 *            0未审核；1已审核
	 * @return MessageResult
	 */
	@RequestMapping(value = "/queryAuditHistory", method = RequestMethod.GET)
	@ApiOperation(value = "",httpMethod = "GET")
	@ApiImplicitParam(paramType = "query", name = "id", value = "状态值(0：未审核/1：已审核)", required = true, dataType = "Integer")
	public MessageResult queryAuditHistory(String id) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		HashMap<String,Object> objs = appScoreService.queryAuditHistory(id);
		result.setData(objs);
		return result;
	}
    
	/**
     * 获取评分最终得分
     *
     * @param sheetid
     * @return
     */
    @RequestMapping(value = "/getLastApproveScore", method = RequestMethod.GET)
    @ApiOperation(value = "获取评分最终得分",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sheetid", value = "量表id", required = true, dataType = "String")})
    public MessageResult getLastApproveScore(String sheetid) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        if (sheetid == null)
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		Score lastApproveScore = appScoreService.getLastApproveScore(sheetid);
		result.setData(lastApproveScore);
        return result;
    }
    /**
     * 获取指标明细
     *
     * @param sheetid
     * @return
     */
    @RequestMapping(value = "/getIndiDetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取右侧指标明细",httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sheetid", value = "量表id", required = true, dataType = "String")})
    public MessageResult getIndiDetail(String id) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        if (id == null)
			throw new AdderBusinessException("参数不能为空","pm.i18n_core.comm.002");
		Map<String,Object> indidetail = appScoreService.getIndiDetail(id);
		result.setData(indidetail);
        return result;
    }
    
	/**
	 * Web端结果审核查看评分中的查询综合评价
	 *
	 * @param sheetid
	 * @return
	 */
	@RequestMapping(value = "/queryApproverComment", method = RequestMethod.GET)
	@ApiOperation(value = "查询综合评价",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", name = "sheetid", value = "量表id", required = true, dataType = "String")})
	public MessageResult queryApproverComment(String sheetid) {
		MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
		// 获取最大轮次
		Integer turn = apprService.getMaxAppraiseTurn(sheetid);
		List<Appraise> apprList = apprService.queryAppraiseComment(sheetid, turn, true);
		result.setData(apprList);
		return result;
	}
}
