package com.engine.custom.corn;

import com.cloudstore.dev.api.bean.MessageBean;
import com.cloudstore.dev.api.bean.MessageType;
import com.cloudstore.dev.api.util.Util_Message;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

public class WeekReportCorn extends BaseCronJob {
    private String userid = "";
    BaseBean baseBean = new BaseBean();
    @Override
    public void execute() {
        writeLog("年度报告开始执行");
        RecordSet recordSet = new RecordSet();

        int startNum = 0;
        int endNum = 0;
        recordSet.executeQuery("select count(1) cnt from xlbb_grzbzz_xda");
        recordSet.next();
        startNum = recordSet.getInt("cnt");
        recordSet.executeUpdate("insert into xlbb_grzbzz_xda(userid,depid,lczs,ybsl, pjhs, zdhs,  zxhs, dbcount, dbzchs ,  pm,insertdate)select userid,depid,lczs,ybsl,round(pjhs,4) as pjhs,zdhs,zxhs,dbcount,dbzchs ,pm   ,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as insertdate from xlbb_grzbzz_xda_view");
        writeLog("执行sql=====>insert into xlbb_grzbzz_xda(userid,depid,lczs,ybsl, pjhs, zdhs,  zxhs, dbcount, dbzchs ,  pm,insertdate)select userid,depid,lczs,ybsl,round(pjhs,4) as pjhs,zdhs,zxhs,dbcount,dbzchs ,pm   ,to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as insertdate from xlbb_grzbzz_xda_view");
        recordSet.executeQuery("select count(1) cnt from xlbb_grzbzz_xda");
        recordSet.next();
        endNum = recordSet.getInt("cnt");

        writeLog("执行sql数量=====>"+recordSet.getCounts());
        sendMsg(startNum,endNum);
    }



    private void writeLog(String log){
        baseBean.writeLog(log);
    }

    private void sendMsg(int startNum ,int endNum){
        MessageType messageType = MessageType.newInstance(1157); // 消息来源（见文档第四点补充 必填）
        Set<String> userIdList = new HashSet<>(); // 接收人id 必填
        String[] useridArr =  userid.split(",");
        for (String s : useridArr) {
            userIdList.add(s);
        }
        String title = "个人周报更新提醒"; // 标题
        String context = "本次周报新增"+(endNum-startNum)+";" +
                "更新前总数"+startNum+";" +
                "更新后总数"+endNum+";"; // 内容
        String linkUrl = ""; // PC端链接
        String linkMobileUrl = ""; // 移动端链接
        try {
            MessageBean messageBean = Util_Message.createMessage(messageType, userIdList, title, context, linkUrl, linkMobileUrl);
            messageBean.setCreater(1);// 创建人id
            //message.setBizState("0");// 需要修改消息为已处理等状态时传入,表示消息最初状态为待处理
            //messageBean.setTargetId("121|22"); //消息来源code +“|”+业务id需要修改消息为已处理等状态时传入
            Util_Message.store(messageBean);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
