package weaver.interfaces.htsc.tgpt;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

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


    @Override
    public void execute() {

        RecordSet rs = new RecordSet();
        RecordSet wbRs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs8 = new RecordSet();
        RecordSet rs10 = new RecordSet();
        RecordSet rs9 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        RecordSet upRs = new RecordSet();
        ArrayList<String> jxCodeList = new ArrayList<String>();
        ArrayList<String> jxList = new ArrayList<>();
        ArrayList<String> titleList = new ArrayList<>();
        ArrayList<String> pathList = new ArrayList<>();
        String sql = "select finducode,finduname,sinducode, sinduname,FELLOWCODE,fellowname,AGENCYCODE,NAMECN from  ( " +
                "select A.finducode,A.finduname,a.sinducode,a.sinduname,b.FELLOWCODE,C.fellowname,C.AGENCYCODE,D.NAMECN from rpt_classification A " +
                " join RPT_RPTFELLOW B on A.REPORTCODE =B.REPORTCODE " +
                " join RPT_RESFELLO C on C.FELLOWCODE =B.FELLOWCODE " +
                " left join RPT_RESAGENCY D on C.AGENCYCODE=D.AGENCYCODE " +
                " )" +
                " group by finducode,finduname,sinducode, sinduname,FELLOWCODE,fellowname,AGENCYCODE,NAMECN";
        rs.execute(sql);
        while(rs.next()){
            String fellowname = rs.getString("fellowname");//外部分析师姓名
            String namecn = rs.getString("NAMECN");//机构名称
            String fellowcode = rs.getString("FELLOWCODE");//机构名称
            if(namecn.contains("华泰")){
                break;

            }
            //到这里就是应该新增的人员
            //?是不是一对一啊
            String table1 = "select * from ZX_STKFORECASTRATING where COMNAME = '"+namecn+"'";
            rs1.execute(table1);
            String invratingcode = null;
            String invratingcodelast = null;
            String comcode = null;
            String ysyqdbqk = "";
            while(rs1.next()){
//                ||rs1.getString("TRADINGCODE").equals("06886")
                if("601688".equals(rs1.getString("TRADINGCODE"))  ){
                    invratingcode = rs1.getString("INVRATINGCODE");//本次评级最新评级
                    int invratingcodeFs = exchangeGrade(invratingcode);
                    invratingcodelast = rs1.getString("INVRATINGCODELAST");//上次评级
                    int invratingcodelastFs = exchangeGrade(invratingcodelast);
                    if(invratingcodeFs >invratingcodelastFs ){
                        ysyqdbqk = "上升";
                    }else if(invratingcodeFs ==invratingcodelastFs){
                        ysyqdbqk = "持平";
                    }else{
                        ysyqdbqk = "下降";
                    }
                    comcode = rs1.getString("COMCODE");//机构代码
                }

            }
            //找到研究院代码
            String table8 = "select * from  EXP_ANALYSTINFO where ANALYSTNAME ='"+fellowname+"' and comcode = '"+comcode+"'";
            rs8.execute(table8);
            String analystcode =null;
            if(rs8.next()){
                analystcode = rs8.getString("ANALYSTCODE");//研究员代码
            }
            //找到奖项code
            String table10 = "select * from EXP_AWARDANALYST where ANALYSTCODE = '"+analystcode+"'";
            rs10.execute(table10);
            String awardcode = null;
            while(rs10.next()){
                jxCodeList.add(rs10.getString("AWARDCODE")) ;//奖项代码
            }
            //找到奖项
            String table9 = "select * from EXP_AWARDINFO where AWARDCODE ='"+awardcode+"'";
            rs9.execute(table9);
            while(rs9.next()){
                switch (rs9.getString("AWARDNAME")){
                    case "1":jxList.add("新财富"); break;
                    case "2":jxList.add("水晶球"); break;
                    case "3":jxList.add("证券业金牛"); break;
                    case "4":jxList.add("金麒麟"); break;
                }
            }
            String collect = jxList.stream().collect(Collectors.joining("|"));//奖项
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime now = LocalDateTime.now();
            String nowStr = now.format(formatter);
            LocalDateTime beforeTime = now.plusYears(-1);
            String beforeTimeStr = beforeTime.format(formatter);
            String table2="select * from ZX_EXPRESEARCHEREPORT  where AUTHOR = '"+fellowname+"' and ORGANIZATIONNAME = '"+namecn+"'  and WRITINGDATE between('"+beforeTimeStr+"','"+nowStr+"')";
            rs2.execute(table2);
            while(rs2.next()){
                if(rs2.getString("TRADINGCODE").equals("601688")||rs2.getString("TRADINGCODE").equals("06886")){
                    titleList.add(rs2.getString("TITLE") );//研报标题

                }

            }
            HashMap<String, String> map = new HashMap<>();
            Iterator<String> iterator = titleList.iterator();
            while(iterator.hasNext()){
                String next = iterator.next();
                String table3 = "select * from ZX_RESEARCHREPORT  where title = '"+next+"'";
                rs3.execute(table3);
                if(rs3.next()){
                    map.put(next,rs3.getString("ANNEXPATH"));
                }

            }
            String selectSql = "select * from uf_wbfxsxxgl where xmcode = '"+fellowcode+"'";
            wbRs.execute(selectSql);
            if(wbRs.next()){
                //更新人员 所有的都要更新
                String id = wbRs.getString("id");
                String updateSql = "update uf_wbfxsxxgl set rzjgmc='"+namecn+"',dgszxpj='"+invratingcode+"',ysyqdbqk='"+ysyqdbqk+"',jynybsl='"+titleList.size()+"',xgryhhjqk='"+collect+"' where xmcode = '"+fellowcode+"'";
                upRs.execute(updateSql);
                String deleteSql = "delete from uf_wbfxsxxgl_dt1 where mainid =   "+id;
                upRs.execute(deleteSql);
                for (Map.Entry en:map.entrySet() ) {

                  String insertSql = "insert into uf_wbfxsxxgl_dt1( mainid , ybdz,ybmc) values("+id+",'"+en.getValue()+"','"+en.getKey()+"')  ";
                    upRs.execute(insertSql);
                }

                break;
            }


            //新增部分代码
            String uuid = UUID.randomUUID().toString();
            String instrSql = "insert into uf_wbfxsxxgl (xm,xmcode,rzjgmc,dgszxpj,ysyqdbqk,jynybsl,xgryhhjqk,uuid)values('"+fellowname+"','"+fellowcode+"','"+namecn+"','"+invratingcode+"','"+ysyqdbqk+"','"+titleList.size()+"','"+collect+"','"+uuid+"')";
            rs.execute(instrSql);
            String newSql ="select * from uf_wbfxsxxgl where uuid = '"+uuid+"'";
            rs.execute(newSql);
            rs.next();
            String id = rs.getString("id");
            for (Map.Entry en:map.entrySet() ) {

                String insertSql = "insert into uf_wbfxsxxgl_dt1( mainid , ybdz,ybmc) values("+id+",'"+en.getValue()+"','"+en.getKey()+"')  ";
                upRs.execute(insertSql);
            }

        }
    }
    public int exchangeGrade(String grade){
        //买入>谨慎买入>增持>中性=持有>减持>卖出
        int fs =0;
        switch (grade){
            case "买入":fs= 6 ; break;
            case "谨慎买入":fs= 5 ;break;
            case "增持":fs= 4 ;break;
            case "中性":fs= 3 ;break;
            case "持有":fs= 3 ;break;
            case "减持":fs= 2 ;break;
            case "卖出":fs= 1 ;break;
        }
        return fs;
    }
}
