package cn.com.countrygarden.bi.main.controller.performance.monthly;

import cn.com.countrygarden.bi.main.core.Response;
import cn.com.countrygarden.bi.main.core.ResultCode;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.performance.PerformanceMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 月度—绩效分析
 */
@RestController
@RequestMapping(value = "/performanceMonth")
public class PerformanceMonthController {

    @Autowired
    private PerformanceMonthService performanceMonthService;

    @Autowired
    private AccessTokenService accessTokenService;



    //-------------------重构月度的绩效分析接口-------------------
    @GetMapping(value = "/performanceAnalysis/{elementId}/aveScore")
    public Object getAveScore(@PathVariable(value = "elementId")long elementId){

        Object result = performanceMonthService.getElementAveScore(elementId);

        return Response.response(result);

    }

    @GetMapping(value = "/performanceAnalysis/{elementId}/monthScore")
    public Object performanceAnalysis(@RequestParam(value = "typeNo") String typeNo,
                                      @PathVariable(value = "elementId") long elementId,
                                      @RequestParam(value = "month") int month,HttpServletRequest request){
        Object result = performanceMonthService.performanceAnalysis(elementId,typeNo,(String)request.getAttribute("userCode"),month);

        return  Response.response(result);
    }

    //-----------------------End-----------------------------------

    //---------------------重构月度的我的考核接口------------------
    @PostMapping(value = "/myPerformance/monthInfo")
    public Object getMonthInfo(@RequestParam(value = "typeNo")String typeNo,
                              @RequestParam(value = "month") int month,HttpServletRequest request,
                              @RequestParam(value = "organList", required = false) String[] selectAreaList){

        Object result = performanceMonthService.myPerformance(selectAreaList,typeNo,(String)request.getAttribute("userCode"),month);

        return Response.response(result);
    }

    //---------------------End------------------------------------


    //月度-绩效分析-区域
    @ResponseBody
    @RequestMapping(value = "/scoreListRegion", method = {RequestMethod.GET, RequestMethod.POST})
    public Object scoreListRegion(@RequestParam(value = "AccessToken") String AccessToken, long elementId) {
        AccessToken token = accessTokenService.findByToken(AccessToken);
        if (token == null || token.getUserCode() == null) {
            return Response.response(ResultCode.UNAUTHORIZED);
        }
        return Response.response(performanceMonthService.scoreListRegion(elementId, token.getUserCode()));
    }

    //月度-绩效分析-城市公司
    @ResponseBody
    @RequestMapping(value = "/scoreListCompany", method = {RequestMethod.GET, RequestMethod.POST})
    public Object scoreListCompany(@RequestParam(value = "AccessToken") String AccessToken, long elementId) {
        AccessToken token = accessTokenService.findByToken(AccessToken);
        if (token == null || token.getUserCode() == null) {
            return Response.response(ResultCode.UNAUTHORIZED);
        }
        return Response.response(performanceMonthService.scoreListCompany(elementId, token.getUserCode()));
    }

}
