package com.api.tjbk;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.wbi.util.ParamUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.StringUtil;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 移动端接口类
 *
 * @author wangj
 * @version 1.00版本
 * @Date 2022/8/22
 */
@Path("/tjbk/MsgTopDialog")
public class TJBKMsgTopDialog {
    private BaseBean bb = new BaseBean();
    private String className = this.getClass().getName()+"-----";

    @GET
    @Path("/getDialog")
    @Produces(MediaType.TEXT_PLAIN)
    public String getVersionInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            // User user = HrmUserVarify.getUser(request, response);
            RecordSet rs = new RecordSet();
            RecordSet recordSet = new RecordSet();
            JSONArray conArr = new JSONArray();
            JSONObject dataObj = new JSONObject();
            // bb.writeLog(className+user);
            // bb.writeLog(className+user.getLoginid());
            Map paramMap = ParamUtil.request2Map(request);
            bb.writeLog(className+paramMap );
            String userid = (String) paramMap.get("userid");
            boolean isdialog = true;
            LocalDate now = LocalDate.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            String nowStr = now.format(formatter);
            String id = "";
            String title = "";
            String btn = "";
            String url = "";
            String backgroundAddress = "";
            String sql = "select * from uf_MsgTopDialog where startDate <= ? and endDate >= ? order by id desc";
            rs.executeQuery(sql, nowStr, nowStr);
            while (rs.next()) {
                String dataid = Util.null2String(rs.getString("id"));
                String scope = Util.null2String(rs.getString("scope"));
                String role = Util.null2String(rs.getString("role"));
                boolean isLatest = getLatestData(dataid, new User(Util.getIntValue(userid)), recordSet, scope, role);
                if (isLatest) {
                    id = Util.null2String(rs.getString("id"));
                    title = Util.null2String(rs.getString("title"));

                    btn = Util.null2String(rs.getString("btn"));
                    url = Util.null2String(rs.getString("url"));
                    backgroundAddress = Util.null2String(rs.getString("backgroundAddress"));
                    break;
                }
            }
            if (!"".equals(id)) {
                sql = "select count(1) as sl from uf_MsgTopDialog_dt2 where mainid = '" + id + "' and hrmid = '" + userid + "'";
                rs.execute(sql);
                while (rs.next()) {
                    if (Util.getIntValue(rs.getString("sl")) > 0) {
                        isdialog = false;
                    }
                }
            } else {
                isdialog = false;
            }
            if (isdialog){
                dataObj.put("id", id);
                dataObj.put("tilte", title);
                dataObj.put("backgroundAddress", backgroundAddress);
                dataObj.put("btn", btn);
                dataObj.put("url", url);
            }
            apidatas.put("data", dataObj);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            bb.writeLog(e.getMessage());
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "getVersionInfo catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    @GET
    @Path("/addDialogUser")
    @Produces(MediaType.TEXT_PLAIN)
    public String addDialogUser(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            //获取当前用户
            // User user = HrmUserVarify.getUser(request, response);
            Map paramMap = ParamUtil.request2Map(request);
            bb.writeLog(className+paramMap );
            String userid = (String) paramMap.get("userid");
            String mainid = Util.null2String(request.getParameter("id"));
            String sql = "insert into uf_MsgTopDialog_dt2 (mainid,hrmid) values ('" + mainid + "','" + userid + "')";
            RecordSet rs = new RecordSet();
            boolean status = rs.execute(sql);
            apidatas.put("status", status);
            apidatas.put("api_status", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("api_status", false);
            apidatas.put("api_errormsg", "addDialogUser catch exception : " + e.getMessage());
        }
        return JSONObject.toJSONString(apidatas);
    }


    private boolean getLatestData(String id, User user, RecordSet recordSet, String scope, String role) {
        HrmUserVarify hrmUserVarify = new HrmUserVarify();

        if ("0".equals(scope)) {
            //角色
            if (!StringUtil.isEmpty(role)) {
                String[] roleArr = role.split(",");
                for (String roleid : roleArr) {
                    boolean b = hrmUserVarify.checkUserRole(roleid, user, user.getUserDepartment() + "");
                    if(b){
                        return true;
                    }
                }
            }
        } else if ("1".equals(scope)) {
            //多人力
            String sql = "select count(main.id) cnt from uf_MsgTopDialog main left join uf_MsgTopDialog_dt1 dt  on main.id = dt.mainid where main.id = ? and dt.hrmid = ?";
            recordSet.executeQuery(sql,id,user.getUID());
            if(recordSet.next()){
                int cnt = recordSet.getInt("cnt");
                if(cnt > 0 ){
                    return true;
                }
            }
        }


        return false;
    }
}
