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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.JSONObject;
import com.yonyou.hrcloud.adder.context.AppContext;
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.ps.model.Sheet;
import com.yonyou.hrcloud.pm.result.model.ResultConfirm;
import com.yonyou.hrcloud.pm.result.service.IResultComfirmService;

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

/**
 * 我的结果控制器
 *
 * @author Flysea
 */
@RestController
@RequestMapping("/pm/result")
@Api(value = "ScoreController | 我的结果控制器")
public class ResultConfirmController extends BaseController {

    @Autowired
    public IResultComfirmService confirmService;

    protected static final Logger logger = LoggerFactory.getLogger(ResultConfirmController.class);

    
    /**
     * Hr 查看认可进展
     */
    @RequestMapping(value = "/resultConfirmProgress", method = RequestMethod.POST)
    @ApiOperation(value = "Hr 查看认可进展",httpMethod = "POST")
    public MessageResult resultConfirmProgress(@RequestBody JSONObject data) {
    	MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
    	HashMap<String,Object> resultmap = new HashMap<String,Object>();
        String schemeid = data.getString("schemeid");
        List<ResultConfirm> resultRecord = confirmService.resultConfirmProgress(schemeid, null);
        int nofb = 0, sure = 0, nosure = 0;
        List<ResultConfirm> nofblst = new ArrayList<ResultConfirm>();
        List<ResultConfirm> surelst = new ArrayList<ResultConfirm>();
        List<ResultConfirm> nosurelst = new ArrayList<ResultConfirm>();
		for(ResultConfirm resultConfirm : resultRecord){
			if(resultConfirm.getConfirm() == ResultConfirm.CONFIRM_STATUS_UNCONFIRMED 
					|| resultConfirm.getLeaderconfirm() == ResultConfirm.CONFIRM_STATUS_UNCONFIRMED){ // 员工和领导有一个没有确认,标记为未确认
				nofb ++;
				nofblst.add(resultConfirm);
			} else if(resultConfirm.getConfirm() == ResultConfirm.CONFIRM_STATUS_APPROVE 
						&& resultConfirm.getLeaderconfirm() == ResultConfirm.CONFIRM_STATUS_APPROVE){ // 员工和领导都认可,标记为认可
				sure ++;
				surelst.add(resultConfirm);
			} else {
				nosure ++;
				nosurelst.add(resultConfirm);
			}
		}
		HashMap<String,Object> nofbmap = new HashMap<String,Object>();
		nofbmap.put("count", nofb);
		nofbmap.put("data", nofblst);
		resultmap.put("nofbmap", nofbmap);
		HashMap<String,Object> suremap = new HashMap<String,Object>();
		suremap.put("count", sure);
		suremap.put("data", surelst);
		resultmap.put("suremap", suremap);
		HashMap<String,Object> nosuremap = new HashMap<String,Object>();
		nosuremap.put("count", nosure);
		nosuremap.put("data", nosurelst);
		resultmap.put("nosure", nosuremap);
        result.setData(resultmap);
        return result;
    }
    
    /**
     * Hr 结束考核
     */
    @RequestMapping(value = "/overResultConfirm", method = RequestMethod.POST)
    @ApiOperation(value = "Hr 结束考核",httpMethod = "POST")
    public MessageResult overResultConfirm(@RequestBody JSONObject data) {
    	MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String schemeid = data.getString("schemeid");
        Boolean isforce = data.getBoolean("isforce");
        if(isforce == null){ // 第一次点击结束考核isforce为false; 如有不符合条件的数据时,弹出框再次点击isforce = true
        	isforce = false;
        }
        JSONObject scheme = confirmService.overResultConfirm(schemeid , isforce);
        result.setMessage(scheme.getString("message"));
        result.setData(scheme);
        return result;
    }
    /**
     * 员工确认绩效结果
     *
     * @return
     */
    @RequestMapping(value = "/staffConfirm", method = RequestMethod.POST)
    @ApiOperation(value = "员工结果确认",httpMethod = "POST")
    public MessageResult staffConfirm(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String staffid = StaffHelper.getStaffByUserId(AppContext.getContext().getUserId());
        String schemeid = data.getString("schemeid");
        Integer state = data.getInteger("state");
        String confirmtext = data.getString("confirmtext");
        confirmService.staffConfirm(schemeid , staffid, state, confirmtext);
        return result;
    }
    
    /**
     * 查看部门确认结果
     *
     * @return
     */
    @RequestMapping(value = "/queryDetpResultByStaffid", method = RequestMethod.POST)
    @ApiOperation(value = "查看部门确认结果",httpMethod = "POST")
    public MessageResult queryDetpResultByStaffid(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        //String schemeid = null;//data.getString("schemeid");
        String staffid = data.getString("staffid");
        List<Sheet> sheetlst = confirmService.queryDetpResultByStaffid(staffid);
        result.setData(sheetlst);
        return result;
    }
    /**
     * 领导确认绩效结果
     *
     * @return
     */
    @RequestMapping(value = "/leaderConfirm", method = RequestMethod.POST)
    @ApiOperation(value = "领导确认绩效结果",httpMethod = "POST")
    public MessageResult leaderConfirm(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        //String staffid = new StaffHelper(staffService)
        //        .getStaffByUserId(AppContext.getContext().getUserId());
        String schemeid = data.getString("schemeid");
        Integer state = data.getInteger("state");
        String staffid = data.getString("staffid");
        String confirmtext = data.getString("confirmtext");
        confirmService.leaderConfirm(schemeid , staffid, state, confirmtext);
        return result;
    }
    
    /**
     * hrbp反馈
     *
     * @return
     */
    @RequestMapping(value = "/hrbpConfirm", method = RequestMethod.POST)
    @ApiOperation(value = "领导确认绩效结果",httpMethod = "POST")
    public MessageResult hrbpConfirm(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String schemeid = data.getString("schemeid");
        //Integer state = data.getInteger("state");
        String staffid = data.getString("staffid");
        String confirmtext = data.getString("confirmtext");
        confirmService.hrbpConfirm(schemeid , staffid, null, confirmtext);
        return result;
    }
    
    
    /**
     * 查看结果反馈
     *
     * @return
     */
    @RequestMapping(value = "/queryResultFedbck", method = RequestMethod.POST)
    @ApiOperation(value = " 查看结果反馈",httpMethod = "POST")
    public MessageResult queryResultFedbck(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String schemeid = data.getString("schemeid");
        String staffid = data.getString("staffid");
        List<Object> fblst = confirmService.queryResultFedbck(schemeid ,staffid);
        result.setData(fblst);
        return result;
    }
    /**
     * 发送结果反馈
     *
     * @return
     */
    @RequestMapping(value = "/sendResultFedbck", method = RequestMethod.POST)
    @ApiOperation(value = " 发送结果反馈",httpMethod = "POST")
    public MessageResult sendResultFedbck(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String schemeid = data.getString("schemeid");
        String text = data.getString("text");
        String staffid = data.getString("staffid");
        confirmService.sendResultFedbck(schemeid ,staffid, text, null);
        return result;
    }
    
    /**
     * 查看反馈状态
     *
     * @return
     */
    @RequestMapping(value = "/queryConfirmState", method = RequestMethod.POST)
    @ApiOperation(value = " 发送结果反馈",httpMethod = "POST")
    public MessageResult queryConfirmState(@RequestBody JSONObject data) {
        MessageResult result = new MessageResult("操作成功!", null, "pm.i18n_core.comm.008");
        String sheetid = data.getString("sheetid");
        Integer queryType = data.getInteger("queryType"); // 0 leader, 1 hr, 2 staff
        ResultConfirm resConfirm = confirmService.queryConfirmState(sheetid, queryType);
        result.setData(resConfirm);
        return result;
    }
    
    
}
