package com.engine.htsc.workflow.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.workflow.util.WorkflowUtil;
import com.engine.workflow.entity.newRequest.WfUser;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.check.JobComInfo;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.share.ShareManager;

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.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class WorkflowCreateAction {


    /**
     * 查询公文流程可创建人
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getCreateUser")
    @Produces({MediaType.TEXT_PLAIN})
    public String getOdocCreateUser(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request,response);
        String workflowid = Util.null2String(request.getParameter("workflowid"));
        String wfunnumber = new PropBean().getWorkflowUnNumberByid(workflowid);
        int htlx = Util.getIntValue(request.getParameter("htlx"));

        try {
            String useridAll = user.getUID() + "";
            String belongtoids = user.getBelongtoids();
            if (!"".equals(belongtoids)) {
                useridAll += "," + belongtoids;
            }
            JobComInfo jobComInfo = new JobComInfo();
            DepartmentComInfo deptComInfo = new DepartmentComInfo();
            List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
            String[] uidArr = useridAll.split(",");
            //ShareManager shareManager = new ShareManager();
            for(int i=0;i<uidArr.length;i++){
                //判断是否有创建流程权限
                User createuser = User.getUser(Integer.parseInt(uidArr[i]),0);
                if (WorkflowUtil.hasOdocWfCreatePermission(htlx,wfunnumber,createuser)) {
                    Map<String,String> item = new HashMap<String,String>();
                    item.put("uID",createuser.getUID()+"");
                    item.put("lastname",createuser.getLastname());
                    item.put("username",createuser.getUsername());
                    item.put("loginid",createuser.getLoginid());
                    item.put("seclevel",createuser.getSeclevel());
                    item.put("sex",createuser.getSex());
                    item.put("userDepartment",createuser.getUserDepartment()+"");
                    item.put("userDeptname",deptComInfo.getDepartmentname(createuser.getUserDepartment()+""));
                    item.put("jobtitle",createuser.getJobtitle());
                    item.put("jobactivity",jobComInfo.getJobName(createuser.getJobtitle()));

                    dataList.add(item);
                }
            }
            apidatas.put("datas", dataList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }



    /**
     * 查询标准流程可创建人
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getWfCreateUser")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCommonWfCreateUser(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request,response);
        String wfunnumber = Util.null2String(request.getParameter("wfunnumber"));
        int workflowid = Integer.parseInt(new PropBean().getActiveWorkflowIdByUnNumber(wfunnumber));

        try {
            String useridAll = user.getUID() + "";
            String belongtoids = user.getBelongtoids();
            if (!"".equals(belongtoids)) {
                useridAll += "," + belongtoids;
            }
            JobComInfo jobComInfo = new JobComInfo();
            DepartmentComInfo deptComInfo = new DepartmentComInfo();
            List<Map<String,String>> dataList = new ArrayList<Map<String,String>>();
            String[] uidArr = useridAll.split(",");
            ShareManager shareManager = new ShareManager();
            for(int i=0;i<uidArr.length;i++){
                //判断是否有创建流程权限
                User createuser = User.getUser(Integer.parseInt(uidArr[i]),0);
                if (shareManager.hasWfCreatePermission(createuser, workflowid)) {
                    Map<String,String> item = new HashMap<String,String>();
                    item.put("uID",createuser.getUID()+"");
                    item.put("lastname",createuser.getLastname());
                    item.put("username",createuser.getUsername());
                    item.put("loginid",createuser.getLoginid());
                    item.put("seclevel",createuser.getSeclevel());
                    item.put("sex",createuser.getSex());
                    item.put("userDepartment",createuser.getUserDepartment()+"");
                    item.put("userDeptname",deptComInfo.getDepartmentname(createuser.getUserDepartment()+""));
                    item.put("jobtitle",createuser.getJobtitle());
                    item.put("jobactivity",jobComInfo.getJobName(createuser.getJobtitle()));

                    dataList.add(item);
                }
            }
            apidatas.put("datas", dataList);
            apidatas.put("wfid", workflowid);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }



    /**
     * 公文发起列表
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getOdocCreateList")
    @Produces({MediaType.TEXT_PLAIN})
    public String getOdocCreateList(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request,response);
        int type = Util.getIntValue(request.getParameter("type"));//1、发文

        try {
            List<Map> datasList = new ArrayList<>();
            String dataSql = "";
            if(type==0 || type==2){//0、发文 2、通知公告
                dataSql = WorkflowUtil.getOdocCreateDataSql(user.getUID(),type);
            }else{//2、收文
                dataSql = " select color,b.id lcid,img,typename,zh,htlx,showorder " +
                        " from uf_gwfq a left join workflow_base b on a.lcid=b.unnumber where isvalid=1 and gwlx="+type;
            }

            if(!"".equals(dataSql)){
                RecordSet rs = new RecordSet();
                rs.execute(dataSql);
                while(rs.next()){
                    Map<String, Object> dataItem = new HashMap<>();
                    String lcid =  Util.null2String(rs.getString("lcid"));
                    String htlx =  Util.null2String(rs.getString("htlx"));
                    dataItem.put("color", Util.null2String(rs.getString("color")));
                    dataItem.put("workflowid",lcid);
                    dataItem.put("img", Util.null2String(rs.getString("img")));
                    dataItem.put("order", Util.null2String(rs.getString("showorder")));
                    dataItem.put("typeName", Util.null2String(rs.getString("typename")));
                    dataItem.put("zh", Util.null2String(rs.getString("zh")));
                    dataItem.put("htlx", htlx);
                    datasList.add(dataItem);
                }
            }

            apidatas.put("datas", datasList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }



    /**
     * 公文看阅分类汇总
     * @param request
     * @param response
     * @return
     */
    @GET
    @Path("/getCountByOdocType")
    @Produces({MediaType.TEXT_PLAIN})
    public String getCountByOdocType(@Context HttpServletRequest request, @Context HttpServletResponse response){

        Map<String, Object> apidatas = new HashMap<String, Object>();
        User user = HrmUserVarify.getUser(request,response);
        try {
            List<Map> datasList = new ArrayList<>();
            String dataSql = WorkflowUtil.getOdocTypeCountSql(user);
            //System.out.println("getCountByOdocType datasql ==========="+dataSql);
            RecordSet rs = new RecordSet();
            rs.execute(dataSql);
            while(rs.next()){
                Map<String, Object> dataItem = new HashMap<>();
                String odoctype =  Util.null2String(rs.getString("id"));
                String count =  Util.null2String(rs.getString("count"));
                String showname =  Util.formatMultiLang(Util.null2String(rs.getString("type_name")));
                dataItem.put("odoctype", odoctype);
                dataItem.put("count",count);
                dataItem.put("showname",showname);
                datasList.add(dataItem);
            }
            apidatas.put("datas", datasList);
            apidatas.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put("success", false);
        }

        return JSONObject.toJSONString(apidatas);
    }



}
