package weaver.interfaces.htsc.tgpt;

import com.alibaba.fastjson.JSONObject;
import com.ibm.icu.text.SimpleDateFormat;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetDataSource;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @Title: ecology-9
 * @Company: 泛微软件
 * @author: K1810039
 * @version: 1.0
 * @CreateDate: 2022-08-19
 * @Description: 投关平台，同步外部分析师 （老的接口）
 * @ModifyLog:
 **/
public class TbwbfxsCron extends BaseCronJob {

    @Override
    public void execute() {
        try{
            JAVATools.writelog("外部分析师定时任务开始");
            PropBean pb = new PropBean();
            String tgpt_wbfxs_formmodeid = pb.getPropName("tgpt_wbfxs_formmodeid");
            ArrayList<String> list = new ArrayList<>();
//            String sql = "select c.namecn ,d.FELLOWNAME,d.fellowcode,d.AGENCYCODE from RPT_RESAGENCY c right join (select a.FELLOWNAME,a.fellowcode,a.AGENCYCODE from RPT_RESFELLO a left join (select distinct(fellowcode) from RPT_RPTFELLOW where REPORTCODE in ( select REPORTCODE from  rpt_classification   where SINDUCODE=490100)) b on a.fellowcode = b.fellowcode ) d on c.AGENCYCODE = d.AGENCYCODE";
            String sql = "select c.namecn ,d.FELLOWNAME,d.fellowcode,d.AGENCYCODE from RPT_RESAGENCY c right join (select a.FELLOWNAME,a.fellowcode,a.AGENCYCODE from RPT_RESFELLO a left join (select distinct(fellowcode) from RPT_RPTFELLOW where REPORTCODE in ( select REPORTCODE from  rpt_classification   where SINDUCODE=490100)) b on a.fellowcode = b.fellowcode ) d on c.AGENCYCODE = d.AGENCYCODE";
//            RecordSet rs = new RecordSet();
            RecordSetDataSource source = new RecordSetDataSource("tgpt_datasource");
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();
            String selectSql = "select * from uf_wbfxsxxgl ";
            rs1.execute(selectSql);
            while (rs1.next()) {
                list.add(rs1.getString("gxfzzd"));
            }
            source.execute(sql);
            while (source.next()) {
                String namecn = source.getString("namecn");//机构名称
                String fellowname = source.getString("fellowname");//分析师姓名
                String fellowcode = source.getString("fellowcode");
                String uuid = UUID.randomUUID().toString();
                Date date = new Date();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
                String dateValue = dateFormat.format(date);
                String time = timeFormat.format(date);
                if (!list.contains(fellowcode)) {
                    //xm,rzjgmc
                    String insetSql = "insert into uf_wbfxsxxgl (xm,rzjgmc,gxfzzd,uuid,modeuuid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime,formmodeid) values ('" + fellowname + "','" + namecn + "','" + fellowcode + "','" + uuid + "','" + uuid + "','" + 1 + "','" + 0 + "','" + dateValue + "','" + time + "','" + tgpt_wbfxs_formmodeid + "')";
                    rs1.execute(insetSql);
                    String mainSql = "select * from uf_wbfxsxxgl where uuid =  '" + uuid + "'";
                    rs2.execute(mainSql);
                    rs2.next();
                    String id = rs2.getString("id");
                    //权限重构
                    ModeRightInfo modeRightInfo = new ModeRightInfo();

                    //DCEAA7A09D441AAAE0538E123FA879C2
                    String rwmodeid = pb.getPropName("WBFXS.MODELCUBEUUID");
                    JSONObject jsonObject_rw = pb.getModeInfoByUUID(rwmodeid);
                    int modeid = Util.getIntValue(jsonObject_rw.getString("modeid"));
                    modeRightInfo.editModeDataShare(1, modeid, Integer.parseInt(id));
                }

            }
            JAVATools.writelog("外部分析师定时任务结束");
        }catch (Exception e ){
            JAVATools.writelog("外部分析师定时任务报错");
            JAVATools.writelog("外部分析师定时任务报错"+e.getMessage());
        }

    }
}
