package com.engine.htsc.duban.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.*;
import java.util.stream.Collectors;

public class TaskTopButtonCmd extends AbstractCommonCommand<Map<String, Object>> {

    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    public TaskTopButtonCmd(Map<String, Object> params, User user) {
        this.params = params;
        this.user = user;
    }

    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        Map<String, Object> appdate = new HashMap<String, Object>();
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();

        Set<String> set = new HashSet<>();
        try{
            String billid = Util.null2String(params.get("billid"));
            String xzzt = Util.null2String(params.get("zt"));
            String userid = user.getUID()+"";
            String mainuserid = "" ;
            String sql =" select id\n" +
                        "  from hrmresource\n" +
                        "  where workcode in (select workcode from hrmresource where id = "+userid+")\n" +
                        "   and belongto is null " ;

            System.out.println(sql);

            rs.execute(sql);
            if(rs.next()){
                mainuserid = Util.null2String(rs.getString("id"));
            }
            System.out.println(mainuserid);
            Map<String,Integer> userMap = queryUserRoleByBillid(mainuserid,billid);
            set.add("share");
            if("0".equals(xzzt)){
                if(userMap.get("cjr") == 1)
                {
                    set.add("edit");
                    set.add("publish");
                }


            }else if("2".equals(xzzt)){
//                if(userMap.get("cjr") == 1){
//                    //审核中
////                    if(userMap.get("ssr")==1 && userMap.get("ssrshzt")==0){
////                    }
////                    set.add("edit");
////                    set.add("publish");
//                }



                if(userMap.get("zxr") == 1)
                {
                    if(userMap.get("ywc") == 1){
                        set.add("reopen");
                    }else{
                        set.add("edit");
                        set.add("complete");
                        set.add("transfer");
                    }
                }

//                if(userMap.get("shr") == 1)
//                {
//                    //审核人 审核进行中
////                    if(userMap.get("shrshzt") != 1){
////                    }
//                    set.add("edit");
//                }

            }else if("3".equals(xzzt)){
                if(userMap.get("zxr") == 1)
                {
                    set.add("reopen");
                }

            }

            /***
             * 草稿　进行中　已完成
             * 创建人、执行人、参与人、审核人、共享人
             *
             * 1、草稿
             * 创建人：编辑、共享、发布
             * 执行人、参与人、审核人:无权限
             * 共享人：可以查看，无按钮
             *
             * 2、进行中
             * 创建人：共享
             * 执行人：编辑、共享、完成
             * 参与人：共享
             * 审核人：共享
             * 共享人：无
             *
             * 3、已完成
             * 创建人：共享
             * 执行人：共享、重新打开
             * 参与人：共享
             * 审核人：共享
             * 共享人：无
             *
             */
            appdate.put("data",set);
            System.out.println(appdate.toString());
        }catch (Exception e){
            bb.writeLog("e:"+e);
        }
        return appdate;
    }


    /***
     *
     * @param userid
     * @param billid
     * @return
     */
    public Map<String,Integer> queryUserRoleByBillid(String userid,String billid){
        RecordSet rs = new RecordSet();

        Map<String,Integer> userMap = new HashMap<String,Integer>();
        int cjrcount = 0;
        String sql = " select id from uf_rw where (instr(','||cjrzczhyc||',','," + userid + ",') >0 or cjr="+userid+") and id=" + billid;
        System.out.println(sql);
        rs.execute(sql);
        if (rs.next()) {
            cjrcount++;
        }
        userMap.put("cjr",cjrcount);

        int zxrcount = 0;
        sql = " select id from uf_rw where (instr(','||jbrzzhyc||',',',"+userid+",') >0  or instr(','||jbr||',',',"+userid+",') >0) and id="+billid ;
        System.out.println(sql);
        rs.execute(sql);
        if(rs.next()){
            zxrcount++;
        }
//        if(zxrcount == 0)
//        {
//            sql = " select id from uf_rw where instr(','||jbr||',',',"+userid+",') >0 and id="+billid ;
//            System.out.println(sql);
//            rs.execute(sql);
//            if(rs.next()){
//                zxrcount++;
//            }
//        }
        userMap.put("zxr",zxrcount);

        int cyrcount = 0;
        sql = " select id from uf_rw where (instr(','||tzydzzhyc||',',',"+userid+",') >0 or instr(','||tzydry||',',',"+userid+",') >0) and id="+billid ;
        System.out.println(sql);
        rs.execute(sql);
        if(rs.next()){
            cyrcount++;
        }
        userMap.put("cyr",cyrcount);


        int gxrcount = 0 ;
        sql = " select id from uf_rw_dt2  where mainid="+billid +" and bgxr="+userid;
        System.out.println(sql);
        rs.execute(sql);
        if(rs.next()){
            gxrcount++;
        }

        userMap.put("gxr",gxrcount);

        int completecount = 0;
        sql = " select id from uf_rw_dt3 where mainid="+billid+" and ry="+userid ;
        System.out.println(sql);
        rs.execute(sql);
        if(rs.next()){
            completecount++;
        }
        userMap.put("ywc",completecount);

//        //审核人
//        int shrcount = 0 ;
//        sql = " select sfsh from uf_rw_dt4  where mainid="+billid +" and instr(','||shr||',',',"+userid+",') >0 ";
//        rs.execute(sql);
//        if(rs.next()){
//            shrcount++;
//            //审核人对应的审核状态
//            userMap.put("shrshzt",rs.getInt("sfsh"));
//        }
//        userMap.put("shr",shrcount);
//
//        //判断是否是送审人
//        int ssrcount = 0;
//        sql = " select sfsh from uf_rw_dt4  where mainid="+billid +" and ry="+userid;
//        rs.execute(sql);
//        if(rs.next()){
//            ssrcount++;
//            //送审人对应的审核状态
//            userMap.put("ssrshzt",rs.getInt("sfsh"));
//        }
//        userMap.put("ssr",ssrcount);


        return userMap;
    }


    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("billid","4435");
        params.put("zt","2");

        User user = new User();
        user.setUid(28302);
        TaskTopButtonCmd TaskTopButtonCmd=new TaskTopButtonCmd(params,user);
        CommandContext ceshi = new CommandContext(null,null);
        TaskTopButtonCmd.execute(ceshi);
    }

}
