package cn.com.countrygarden.bi.main.util;

import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.performance.AssessmentTarget;
import cn.com.countrygarden.bi.main.model.performance.UserTarget;
import cn.com.countrygarden.bi.main.model.performance.monthly.AssessmentMonthlyTarget;
import cn.com.countrygarden.bi.main.service.AccessTokenService;
import cn.com.countrygarden.bi.main.service.performance.AssessmentMonthlyTargetService;
import cn.com.countrygarden.bi.main.service.performance.AssessmentTargetService;
import cn.com.countrygarden.bi.main.service.performance.UserTargetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 绩效—权限相关
 *
 * @author Max
 * @version 1.0
 * @since 1.8
 */
@Service
public class PerformancePower {

    @Autowired
    private AssessmentTargetService assessmentTargetService;
    @Autowired
    private AccessTokenService accessTokenService;
    @Autowired
    private UserTargetService userTargetService;
    @Autowired
    private AssessmentMonthlyTargetService assessmentMonthlyTargetService;

    /**
     * 季度考核权限相关
     *
     * @param elementId  指标id
     * @param requestMap 存放years,quarterly,handle,lockstatus
     * @param request    Request
     * @return 权限等数据
     */
    public Map<String, Integer> getUserPower(String elementId, Map<String, Object> requestMap, HttpServletRequest request) {
        try {
            AssessmentTarget assessmentTarget = new AssessmentTarget();
            assessmentTarget.setElementid(elementId);
            assessmentTarget.setYear((Integer) requestMap.get("years"));//年
            assessmentTarget.setQuarterly(((Integer) requestMap.get("quarter")).toString());//季度
            Map<String, Integer> map = new HashMap<>();
            int handle = (Integer) requestMap.get("handle");
            int lockstatus = (Integer) requestMap.get("lockstatus");
            //查询操作
            AssessmentTarget result_assessment = assessmentTargetService.selectJudgeLock(assessmentTarget);
            if (result_assessment != null) {
                //更新操作
                if (handle == 2) {
                    if (lockstatus == 0) {
                        result_assessment.setLockstatus(1);
                    } else {
                        result_assessment.setLockstatus(0);
                    }
                    //执行更新操作
                    assessmentTargetService.updateByPrimaryKeySelective(result_assessment);
                }
                map.put("lockstatus", result_assessment.getLockstatus());
            } else {
                map.put("lockstatus", 1);//无数据，进行锁定
            }
            //得到session中的AccessToken
            String accessToken = request.getParameter("AccessToken");
            AccessToken token = accessTokenService.findByToken(accessToken);
            //查询是否有权限
            UserTarget userTarget = new UserTarget();
            userTarget.setUsercode(token.getUserCode());
            userTarget.setElementid(elementId);
            UserTarget result_userTarget = userTargetService.selectCouldPower(userTarget);
            if (result_userTarget != null) {
                //查询(0:表示无权;1:表示有权)
                map.put("couldRead", result_userTarget.getCouldread());
                //保存/导入(0:表示无权;1:表示有权)
                map.put("couldWrite", result_userTarget.getCouldwrite());
                //锁定(0:表示无权;1:表示有权)
                map.put("couldLock", result_userTarget.getCouldlock());
                //解锁(0:表示无权;1:表示有权)
                map.put("couldUnLock", result_userTarget.getCouldunlock());
            } else {
                //无数据，不开放权限，全部为无权
                map.put("couldRead", 0);
                map.put("couldWrite", 0);
                map.put("couldLock", 0);
                map.put("couldUnLock", 0);
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Integer> map = new HashMap<>();
            map.put("lockstatus", 1);//锁定
            //无权限
            map.put("couldRead", 0);
            map.put("couldWrite", 0);
            map.put("couldLock", 0);
            map.put("couldUnLock", 0);
            return map;
        }
    }

    /**
     * 月度考核权限相关
     *
     * @param requestMap 存放element,years,quarterly,handle,lockstatus
     * @param request    Request
     * @return 权限等数据
     */
    public Map<String, Integer> getUserPowerMonthly(Map<String, Object> requestMap, HttpServletRequest request) {
        try {
            //获取前端传过来的参数
            long elementId = (long)(Integer) requestMap.get("elementId");//指标id
            int year = (Integer) requestMap.get("year");//年
            int month = (Integer) requestMap.get("month");//月
            int handle = (Integer) requestMap.get("handle");
            int lockStatus = (Integer) requestMap.get("lockStatus");

            //存放返回结果
            Map<String, Integer> result = new HashMap<>();

            //查询当前指标当前月度是否为锁定状态
            AssessmentMonthlyTarget result_assessment = assessmentMonthlyTargetService.selectJudgeLock(elementId, year, month);

            if (result_assessment != null) {
                //如果handle=2,执行更新操作
                if (handle == 2) {
                    if (lockStatus == 0) {
                        result_assessment.setLockStatus(1);
                    } else {
                        result_assessment.setLockStatus(0);
                    }
                    //执行更新操作
                    assessmentMonthlyTargetService.updateByPrimaryKeySelective(result_assessment);
                }
                result.put("lockStatus", result_assessment.getLockStatus());//存储当前最新的数据
            }else {
                result.put("lockStatus", 1);//无数据，进行锁定
            }
            //得到session中的AccessToken
            String accessToken = request.getParameter("AccessToken");
            AccessToken token = accessTokenService.findByToken(accessToken);
            //查询是否有权限
            UserTarget userTarget = new UserTarget();
            userTarget.setUsercode(token.getUserCode());
            userTarget.setElementid(String.valueOf(elementId));
            UserTarget result_userTarget = userTargetService.selectCouldPower(userTarget);
            if (result_userTarget != null) {
                result.put("couldUnLock", result_userTarget.getCouldunlock());//解锁(0:表示无权;1:表示有权)
                result.put("couldRead", result_userTarget.getCouldread());//查询(0:表示无权;1:表示有权)
                result.put("couldWrite", result_userTarget.getCouldwrite());//保存/导入(0:表示无权;1:表示有权)
                result.put("couldLock", result_userTarget.getCouldlock());//锁定(0:表示无权;1:表示有权)
            } else {
                //无数据，不开放权限，全部为无权
                result.put("couldRead", 0);
                result.put("couldWrite", 0);
                result.put("couldLock", 0);
                result.put("couldUnLock", 0);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Integer> result = new HashMap<>();
            result.put("lockStatus", 1);//锁定
            //无权限
            result.put("couldUnLock", 0);
            result.put("couldRead", 0);
            result.put("couldWrite", 0);
            result.put("couldLock", 0);
            return result;
        }
    }
}
