package com.engine.htsc.cowork.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.*;

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


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

    public CoWorkTopButtonCmd(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();
        RecordSet stopOrComRs =  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 " ;
            rs.execute(sql);
            if(rs.next()){
                mainuserid = Util.null2String(rs.getString("id"));
            }

            Map<String,Integer> userMap = queryUserRoleByBillid(mainuserid,billid);
            //2022-01-25
            String stopOrCompleteSql = "select * from uf_xzsy where instr(','||cjrzczhyc||',',',"+mainuserid+",') >0 and id = "+billid + " and (xzzt = 5 or xzzt = 6) ";
            bb.writeLog("stopOrCompleteSql:"+stopOrCompleteSql);
            stopOrComRs.execute(stopOrCompleteSql);
//            System.out.println(stopOrCompleteSql);
//            System.out.println(stopOrComRs.execute(stopOrCompleteSql));
//            System.out.println(stopOrComRs.next());
            if(stopOrComRs.next()){
                set.add("share");
            }else{
                set.add("share");
                if("0".equals(xzzt)){
                    if(userMap.get("cjr") == 1)
                    {
                        set.add("edit");
                        set.add("publish");
                        set.add("auditing");
                        //2022-01-25
//                        set.add("complete");//完成
                        set.add("stop");//终止
                    }

                    if(userMap.get("shr") == 1 && userMap.get("shrshzt")==0 ){
                        set.add("edit");
                        set.add("read");
                        set.add("shrTransfer");
                    }
                }else if("2".equals(xzzt)){
                    if(userMap.get("cjr") == 1){
                        set.add("stop");//终止
                        //审核中
                        if(userMap.get("ssr")==1 && userMap.get("ssrshzt")==0){
                            set.add("edit");
                            set.add("publish");
                            set.add("auditing");
                        }
                    }
                    if(userMap.get("zxr") == 1)
                    {
                        if(userMap.get("ywc") == 1){
                            set.add("reopen");
                        }else{
                            set.add("edit");
                            set.add("complete");
                            set.add("auditing");
                            set.add("transfer");
                        }
                    }

                    if(userMap.get("shr") == 1)
                    {
                        //审核人 审核进行中
                        if(userMap.get("shrshzt") != 1){
                            set.add("edit");
                            set.add("read");
                            //2022-02-25
                            set.add("shrTransfer");
                        }
                    }
                }else if("3".equals(xzzt)){
                    if(userMap.get("zxr") == 1)
                    {
                        set.add("reopen");
                    }


                }


            }

            /***
             * 草稿　进行中　已完成
             * 创建人、执行人、参与人、审核人、共享人
             *
             * 1、草稿
             * 创建人：编辑、共享、发布
             * 执行人、参与人、审核人:无权限
             * 共享人：可以查看，无按钮
             *
             * 2、进行中
             * 创建人：共享
             * 执行人：编辑、共享、完成
             * 参与人：共享
             * 审核人：共享
             * 共享人：无
             *
             * 3、已完成
             * 创建人：共享
             * 执行人：共享、重新打开
             * 参与人：共享
             * 审核人：共享
             * 共享人：无
             *
             */
        }catch (Exception e){
            bb.writeLog("e:"+e);
            set.add("share");
        }
        appdate.put("data",set);
        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_xzsy where (instr(','||cjrzczhyc||',',',"+userid+",') >0  or cjr="+userid+")and id="+billid ;
        rs.execute(sql);
        if(rs.next()){
            cjrcount++;
        }
        userMap.put("cjr",cjrcount);

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

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


        int gxrcount = 0 ;
        sql = " select id from uf_xzsy_dt1  where mainid="+billid +" and bgxr="+userid;
        rs.execute(sql);
        if(rs.next()){
            gxrcount++;
        }
        userMap.put("gxr",gxrcount);

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


        //审核人
        int shrcount = 0 ;
        int shrshzt = 0;
        sql = " select sfsh from uf_xzsy_dt3  where mainid="+billid +" and instr(','||shr||',',',"+userid+",') >0 order by id desc";
        rs.execute(sql);
        if(rs.next()){
            shrcount++;
            //审核人对应的审核状态
            shrshzt = rs.getInt("sfsh");

        }
        userMap.put("shrshzt",shrshzt);
        userMap.put("shr",shrcount);

        //判断是否是送审人
        int ssrcount = 0;
        int ssrshzt = 0;
        sql = " select sfsh from uf_xzsy_dt3  where mainid="+billid +" and ry="+userid+" order by id desc";
        rs.execute(sql);
        if(rs.next()){
            ssrcount++;
            //送审人对应的审核状态
            ssrshzt = rs.getInt("sfsh");
        }
        userMap.put("ssrshzt",ssrshzt);
        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","4732");
        params.put("zt","1");
        params.put("userid","011413");
        User user = new User(11260);
        CoWorkTopButtonCmd coWorkTopButtonCmd = new CoWorkTopButtonCmd(params,user);
        CommandContext ceshi= new CommandContext(null,null);
        coWorkTopButtonCmd.execute(ceshi);

    }

}
