package weaver.interfaces.zjrb.mode;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import oracle.sql.CLOB;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.ConnStatement;
import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.RequestInfo;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class ThreeBatchEditModeExpand extends AbstractModeExpandJavaCodeNew {

    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        try {
            User user = (User)param.get("user");
            int billid = -1;//数据id
            int modeid = -1;//模块id
            RequestInfo requestInfo = (RequestInfo)param.get("RequestInfo");
            if(requestInfo!=null){
                billid = Util.getIntValue(requestInfo.getRequestid());
                modeid = Util.getIntValue(requestInfo.getWorkflowid());
                if(billid>0&&modeid>0){
                    //------请在下面编写业务逻辑代码------
                    String roleid = "29570";
                    int code = 1337;
                    int creater = 1 ;
                    sendRemindByEdit(billid+"",roleid,code,creater);
                }
            }
        } catch (Exception e) {
            result.put("errmsg","自定义出错信息");
            result.put("flag", "false");
        }
        return result;
    }

    public void sendRemindByEdit(String billid,String roleid,int code,int creater){

        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        ResourceComInfo resourceComInfo = null;
        try{
            resourceComInfo = new ResourceComInfo();

            if(StringUtils.isNotBlank(billid)){

                /**
                 * 0:政保类、1:商业类
                 */

                String ywlx = "";
                String proname = "";
                String sql =" select ywlx,proname from uf_threebatches where id="+billid+" ";
                rs.executeQuery(sql);
                if(rs.next()){
                    ywlx = Util.null2String(rs.getString("ywlx"));
                    proname = Util.null2String(rs.getString("proname"));
                }
                String citycom = "";
                if("0".equals(ywlx)){

                    /**
                     * 政保类 50w以上的
                     */
                    sql =" select id,citycom,modedatacreater from uf_threebatches where id="+billid+" and proamout > 50 and sfcj = 1 ";
                    rs.executeQuery(sql);
                    if(rs.next()){
                        citycom = Util.null2String(rs.getString("citycom"));
                    }
                    if(StringUtils.isNotBlank(citycom)){
                        Set<String> userSet = new HashSet<String>();
                        String lastname = "";
                        sql = " select resourceid from hrmrolemembers where roleid = 29570 and resourcetype=1 and resourceid in(select id from hrmresource where subcompanyid1 in("+citycom+"))" ;
                        rs.executeQuery(sql);
                        bb.writeLog("查找角色:三个一批项目经理[29570]中的人员的分部为表单[立项市公司]的:"+sql+"</br>");
                        while (rs.next()){
                            String resourceid = Util.null2String(rs.getString("resourceid"));
                            userSet.add(resourceid);
                            lastname += resourceComInfo.getLastname(resourceid)+",";
                        }
                        
                        if(userSet.size() > 0){
                            String context = "请及时维护"+proname+"的落地情况";
                            String title = "三个一批项目提醒";
                            bb.writeLog("建模ID:"+billid+" ,项目:"+proname+" ,政保类项目 ,项目金额 > 50 ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                            senNotice(code,title,context,userSet,billid,creater);

                        }
                    }

                    /**
                     * 政保类 100w以上的
                     */
                    int count = 0 ;
                    sql =" select id,citycom,modedatacreater from uf_threebatches where id="+billid+" and proamout > 100 and sfcj = 1 ";
                    rs.executeQuery(sql);
                    if(rs.next()){
                        count++;
                    }
                    if(count > 0){
                        Set<String> userSet = new HashSet<String>();
                        String lastname = "";
                        sql =   " select resourceid \n" +
                                " from hrmrolemembers " +
                                " where roleid = 29570 " +
                                " and resourcetype=1 \n" +
                                " and resourceid in(\n" +
                                "    select id from hrmresource where departmentid in(\n" +
                                "        select t.id \n" +
                                "        from hrmdepartment t\n" +
                                "        inner join hrmsubcompany k on k.id = t.subcompanyid1\n" +
                                "        where k.subcompanycode='33000000'\n" +
                                "        and nvl(t.canceled,0)=0\n" +
                                "    )\n" +
                                " ) " ;
                        bb.writeLog("查找角色:三个一批项目经理[29570]中的人员的分部为总部的:"+sql+"</br>");
                        rs.executeQuery(sql);
                        while (rs.next()){
                            String resourceid = Util.null2String(rs.getString("resourceid"));
                            userSet.add(resourceid);

                            lastname += resourceComInfo.getLastname(resourceid)+",";
                        }

                        if(userSet.size() > 0){
                            String context = "请及时维护"+proname+"的落地情况";
                            String title = "三个一批项目提醒";
                            bb.writeLog("建模ID:"+billid+" ,项目:"+proname+" ,政保类项目 ,项目金额 > 100 ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                            senNotice(code,title,context,userSet,billid,creater);
                        }
                    }
                }else if("1".equals(ywlx)){
                    /**
                     * 商业类 100w以上的
                     */
                    sql =" select id,citycom,modedatacreater from uf_threebatches where id="+billid+" and proamout > 100 and sfcj = 1 ";
                    rs.executeQuery(sql);
                    if(rs.next()){
                        citycom = Util.null2String(rs.getString("citycom"));
                    }
                    if(StringUtils.isNotBlank(citycom)){
                        Set<String> userSet = new HashSet<String>();
                        String lastname = "";
                        sql = " select resourceid from hrmrolemembers where roleid = 29570 and resourcetype=1 and resourceid in(select id from hrmresource where subcompanyid1 in("+citycom+"))" ;
                        bb.writeLog("查找角色:三个一批项目经理[29570]中的人员的分部为表单[立项市公司]的:"+sql+"</br>");
                        rs.executeQuery(sql);
                        while (rs.next()){
                            String resourceid = Util.null2String(rs.getString("resourceid"));
                            userSet.add(resourceid);
                            lastname += resourceComInfo.getLastname(resourceid)+",";
                        }
                        if(userSet.size() > 0){
                            String context = "请及时维护"+proname+"的落地情况";
                            String title = "三个一批项目提醒";
                            bb.writeLog("建模ID:"+billid+" ,项目:"+proname+" ,商业类项目 ,项目金额 > 100 ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                            senNotice(code,title,context,userSet,billid,creater);

                        }
                    }

                    int count = 0 ;
                    sql =" select id,citycom,modedatacreater from uf_threebatches where id="+billid+" and proamout > 150 and sfcj = 1 ";
                    rs.executeQuery(sql);
                    if(rs.next()){
                        count++;
                    }
                    if(count > 0){
                        String lastname = "";
                        Set<String> userSet = new HashSet<String>();
                        sql =   " select resourceid \n" +
                                " from hrmrolemembers " +
                                " where roleid = 29570 " +
                                " and resourcetype=1 \n" +
                                " and resourceid in(\n" +
                                "    select id from hrmresource where departmentid in(\n" +
                                "        select t.id \n" +
                                "        from hrmdepartment t\n" +
                                "        inner join hrmsubcompany k on k.id = t.subcompanyid1\n" +
                                "        where k.subcompanycode='33000000'\n" +
                                "        and nvl(t.canceled,0)=0\n" +
                                "    )\n" +
                                " ) " ;

                        bb.writeLog("查找角色:三个一批项目经理[29570]中的人员的分部为总部的:"+sql+"</br>");
                        rs.executeQuery(sql);
                        while (rs.next()){
                            String resourceid = Util.null2String(rs.getString("resourceid"));
                            userSet.add(resourceid);

                            lastname += resourceComInfo.getLastname(resourceid)+",";
                        }
                        if(userSet.size() > 0){
                            String context = "请及时维护"+proname+"的落地情况";
                            String title = "三个一批项目提醒";
                            bb.writeLog("建模ID:"+billid+" ,项目:"+proname+" ,商业类项目 ,项目金额 > 150 ,提醒标题:"+title+" ,提醒内容:"+context+" ,提醒人:"+lastname+"("+userSet.toString()+")</br>");

                            senNotice(code,title,context,userSet,billid,creater);
                        }
                    }
                }
            }
        }catch (Exception e){
            bb.writeLog("e:"+e+"</br>");
        }
    }


    public void senNotice(int code,String title,String context,Set<String> userSet,String billid,int creater){
        BaseBean bb = new BaseBean();
        if(userSet.size() > 0){
            /**
             * 消息来源（见文档第四点补充 必填）
             */
            MessageType messageType = MessageType.newInstance(code);
            /**
             * PC端链接
             */
            String linkUrl = "/spa/cube/index.html#/main/cube/card?type=0&modeId=26008&billid="+billid;
            /**
             * 移动端链接
             */
            String linkMobileUrl = "/mobilemode/mobile/view.html?appid=24501#&page_33506?billid="+billid;

            try {
                MessageBean messageBean = Util_Message.createMessage(messageType, userSet, title, context, linkUrl, linkMobileUrl);
                /**
                 * 创建人id
                 */
                messageBean.setCreater(creater);
                boolean isTrue = Util_Message.store(messageBean);
                bb.writeLog("MicroExaminationNoticeSendJob-senNotice:"+isTrue);
            } catch (IOException e) {
                bb.writeLog("MicroExaminationNoticeSendJob-e3:"+e);
            }
        }
    }

}
