package com.api.hrm.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.alibaba.fastjson.JSONObject;
import com.api.hrm.service.HrmFinanceService;
import com.engine.common.biz.EncryptConfigBiz;
import com.engine.common.enums.EncryptMould;
import com.engine.common.util.ParamUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

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

/**
 * 薪资福利
 */
@Path("/hrm/finance")
public class HrmFinanceAction extends BaseBean {
    /**
     * 是否有权限
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHasRight")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHasRight(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            User user = HrmUserVarify.getUser (request , response) ;
            String id = Util.null2String(request.getParameter("id"));
            boolean hasRight = HrmUserVarify.checkUserRight("HrmResourceWelfareEdit:Edit", user);
            if(!hasRight){
                hasRight = id.length()>0 && id.equals(""+user.getUID());
            }
            if(!hasRight && id.length()==0){
                //id=0 默认是自己
                hasRight = true;
            }
            apidatas.put("hasRight", hasRight);
            apidatas.put("status", "1");
        } catch (Exception e) {
            apidatas.put("status", "-1");
            writeLog(e);
        }
        return JSONObject.toJSONString(apidatas);
    }
    /**
     * 获取tab
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getTabInfo")
    @Produces(MediaType.TEXT_PLAIN)
    public String getTabInfo(@Context HttpServletRequest request, @Context HttpServletResponse response){
        User user = HrmUserVarify.getUser(request, response);
        String data = "";
        boolean checkSecondAuthToken = new EncryptConfigBiz().checkSecondAuthToken(EncryptMould.HRM.getCode(), "SALARY",request);
        if (!checkSecondAuthToken) {
            Map<String, Object> retmap = new HashMap<String, Object>();
            retmap.put("status", "-1");
            retmap.put("message", SystemEnv.getHtmlLabelName(2012, weaver.general.ThreadVarLanguage.getLang()));
            data = JSONObject.toJSONString(retmap);
        } else {
            data = new HrmFinanceService().getTabInfo(request, response);
        }
        return data;
    }

    /**
     * 获取薪资福利表单字段
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHrmFinanceFields")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHrmFinanceFields(@Context HttpServletRequest request, @Context HttpServletResponse response){
/*    String data = "";
    boolean checkSecondAuthToken = new EncryptConfigBiz().checkSecondAuthToken(EncryptMould.HRM.getCode(), "SALARY",request);
    if (!checkSecondAuthToken) {
      Map<String, Object> retmap = new HashMap<String, Object>();
      retmap.put("status", "-1");
      retmap.put("message", SystemEnv.getHtmlLabelName(2012, weaver.general.ThreadVarLanguage.getLang()));
      data = JSONObject.toJSONString(retmap);
    } else {
      data = new HrmFinanceService().getHrmFinanceFields(request, response);
    }*/
        return new HrmFinanceService().getHrmFinanceFields(request, response);
    }

    /**
     * 获取最新工资单
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getHrmResourceSalaryLog")
    @Produces(MediaType.TEXT_PLAIN)
    public String getHrmResourceSalaryLog(@Context HttpServletRequest request, @Context HttpServletResponse response){
        User user = HrmUserVarify.getUser(request, response);
        String data = "";
//    boolean checkSecondAuthToken = new EncryptConfigBiz().checkSecondAuthToken(EncryptMould.HRM.getCode(), "SALARY",request);
//    if (!checkSecondAuthToken) {
//      Map<String, Object> retmap = new HashMap<String, Object>();
//      retmap.put("status", "-1");
//      retmap.put("message", SystemEnv.getHtmlLabelName(2012, weaver.general.ThreadVarLanguage.getLang()));
//      data = JSONObject.toJSONString(retmap);
//    } else {
        String tabkey = Util.null2String(request.getParameter("tabkey"));
        if(tabkey.equals("1")){
            data = new HrmFinanceService().getHrmResourceSalaryLog(request, response);
        }else if(tabkey.equals("2")){
            data = new HrmFinanceService().HrmResourceSalaryList(request, response);
        }else if(tabkey.equals("3")){
            data = new HrmFinanceService().HrmResourceChangeLog(request, response);
        }
//    }
        return data;
    }

    /**
     * 获取薪资福利表单字段
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.TEXT_PLAIN)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response){
        String data = new HrmFinanceService().save(request, response);
        return data;
    }

    /**
     * 查看工资时验证二次密码表单
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getSecondaryPasswordForm")
    @Produces(MediaType.TEXT_PLAIN)
    public String getSecondaryPasswordForm(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            apidatas = new HrmFinanceService().getSecondaryPasswordForm(request, response);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("status", "-1");
            apidatas.put("message", e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 查看工资时是否需要二次密码验证
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/isNeedSecondPwdVerify")
    @Produces(MediaType.TEXT_PLAIN)
    public String isNeedSecondPwdVerify(@Context HttpServletRequest request, @Context HttpServletResponse response){
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            apidatas = new HrmFinanceService().isNeedSecondPwdVerify(request, response);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("status", "-1");
            apidatas.put("message", e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }

    /**
     * 二次密码校验
     * @param request
     * @param response
     * @return
     */
    @POST
    @Path("/checkSecondPasword")
    @Produces(MediaType.TEXT_PLAIN)
    public String checkSecondPasword(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try{
            apidatas = new HrmFinanceService().checkSecondPasword(request, response);
        }catch (Exception e){
            e.printStackTrace();
            apidatas.put("status", "-1");
            apidatas.put("message", e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }
}
