package com.trs.web2frame.tool.tyjyh.ggzyzheng;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.trs.web2frame.tool.tyjyh.ConnectionUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ColumnMapping {

    public static String CC_TARGET_CHANNEL_ID = "";

    public static DemoData demoDataStatic  = null;

    public List<ColumnMapData> getCmsCarbonCopy(String manuscriptId) {
        // 获取文档相关的附件信息
        List<ColumnMapData> columnMapDataList = new ArrayList<ColumnMapData>();

        try {

            Connection connection = null;
            Statement statement = null;
            ResultSet set = null;
            try {
                StringBuilder sb=new StringBuilder(
                        "select * from CMS_CARBON_COPY where MANUSCRIPT_ID = '"+manuscriptId+"'");
                connection = ConnectionUtil.getConnection("mysql");
                statement = connection.createStatement();
                System.out.println(sb);
                set = statement.executeQuery(sb.toString());
                while (set.next()){
                    ColumnMapping.CC_TARGET_CHANNEL_ID = "";
                    ColumnMapData columnData = new ColumnMapData();
                    String MANUSCRIPT_ID = set.getString("MANUSCRIPT_ID");
                    if(MANUSCRIPT_ID != null && MANUSCRIPT_ID != ""){
                        columnData.setMANUSCRIPT_ID(MANUSCRIPT_ID);
                    }
                    String CC_TARGET_CHANNEL_ID = set.getString("CC_TARGET_CHANNEL_ID");
                    if(CC_TARGET_CHANNEL_ID != null && CC_TARGET_CHANNEL_ID != ""){
                        columnData.setCC_TARGET_CHANNEL_ID(CC_TARGET_CHANNEL_ID);
                        ColumnMapping.CC_TARGET_CHANNEL_ID = CC_TARGET_CHANNEL_ID;
                    }
                    String MANUSCRIPT_SEQ_NUM = set.getString("MANUSCRIPT_SEQ_NUM");
                    if(MANUSCRIPT_SEQ_NUM != null && MANUSCRIPT_SEQ_NUM != ""){
                        columnData.setMANUSCRIPT_SEQ_NUM(MANUSCRIPT_SEQ_NUM);
                    }
                    String USER_ID = set.getString("USER_ID");
                    if(USER_ID != null && USER_ID != ""){
                        columnData.setUSER_ID(USER_ID);
                    }
                    String CC_TIME = set.getString("CC_TIME");
                    if(CC_TIME != null && CC_TIME != ""){
                        columnData.setCC_TIME(CC_TIME);
                    }
                    String STATUS = set.getString("STATUS");
                    if(STATUS != null && STATUS != ""){
                        columnData.setSTATUS(STATUS);
                    }
                    String IS_MODIFIED_SEQ_NUM = set.getString("IS_MODIFIED_SEQ_NUM");
                    if(IS_MODIFIED_SEQ_NUM != null && IS_MODIFIED_SEQ_NUM != ""){
                        columnData.setIS_MODIFIED_SEQ_NUM(IS_MODIFIED_SEQ_NUM);
                    }
                    String SORTED_TIME = set.getString("SORTED_TIME");
                    if(SORTED_TIME != null && SORTED_TIME != ""){
                        columnData.setSORTED_TIME(SORTED_TIME);
                    }
                    String RELATION = set.getString("RELATION");
                    if(RELATION != null && RELATION != ""){
                        columnData.setRELATION(RELATION);
                    }
                    String WEBSITE_ID = set.getString("WEBSITE_ID");
                    if(WEBSITE_ID != null && WEBSITE_ID != ""){
                        columnData.setWEBSITE_ID(WEBSITE_ID);
                    }
                    String BATCH_NUMBER = set.getString("BATCH_NUMBER");
                    if(BATCH_NUMBER != null && BATCH_NUMBER != ""){
                        columnData.setBATCH_NUMBER(BATCH_NUMBER);
                    }
                    String TITLE = set.getString("TITLE");
                    if(TITLE != null && TITLE != ""){
                        columnData.setTITLE(TITLE);
                    }
                    String SUB_TITLE = set.getString("SUB_TITLE");
                    if(SUB_TITLE != null && SUB_TITLE != ""){
                        columnData.setSUB_TITLE(SUB_TITLE);
                    }
                    String TYPE = set.getString("TYPE");
                    if(TYPE != null && TYPE != ""){
                        columnData.setTYPE(TYPE);
                    }
                    String MEMO = set.getString("MEMO");
                    if(MEMO != null && MEMO != ""){
                        columnData.setMEMO(MEMO);
                    }
                    String CARBONCOPY_ID = set.getString("CARBONCOPY_ID");
                    if(CARBONCOPY_ID != null && CARBONCOPY_ID != ""){
                        columnData.setCARBONCOPY_ID(CARBONCOPY_ID);
                    }
                    System.out.println(columnData);
                    try {
                        //读取Excel，获取对于原本海运的栏目ID
                        EasyExcel.read(ColumnConfig.fileName2, DemoData.class, new PageReadListener<DemoData>(dataList -> {
                            for (DemoData demoData : dataList) {
                                String cc_target_channel_id = ColumnMapping.CC_TARGET_CHANNEL_ID;
                                System.out.println(cc_target_channel_id);
                                String kpChannelId = demoData.getKpChannelId();
                                if(cc_target_channel_id != null && cc_target_channel_id != null){
                                    if(kpChannelId != null && kpChannelId != null){
                                        if(kpChannelId.equals(cc_target_channel_id)){
                                            ColumnMapping.demoDataStatic = demoData;
                                        }
                                    }
                                }
                            }
                        })).sheet().doRead();
                    }catch (Exception ee){
                        ee.printStackTrace();
                    }
                    DemoData demoDataStatic = ColumnMapping.demoDataStatic;
                    if(demoDataStatic != null){
                        columnData.setDemoData(demoDataStatic);
                    }
                    System.out.println(columnData);
                    columnMapDataList.add(columnData);
                }
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            } finally {
                ConnectionUtil.closeConnection(set, statement, connection);
            }
            return columnMapDataList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void ColumnMappingCheck(String manuscriptId){
        List<ColumnMapData> columnMapDataList = getCmsCarbonCopy(manuscriptId);
        for (ColumnMapData columnMapData : columnMapDataList) {
            DemoData demoData = columnMapData.getDemoData();
            if (demoData != null){
                System.out.println(demoData);
                String view = demoData.getView();
                ViewMoveTow viewMovetow = new ViewMoveTow();
                if(view != null && view != ""){
                    if(view.equals("1")){
                        try {
                            viewMovetow.DataZhiXunByChannel(demoData.getKpChannelId(),demoData.getHyChannelId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if(view.equals("2")){
                        try {
                            viewMovetow.DataZWGKByChannel(demoData.getKpChannelId(),demoData.getHyChannelId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if(view.equals("3")){
                        try {
                            viewMovetow.DataZFGBByChannel(demoData.getKpChannelId(),demoData.getHyChannelId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    if(view.equals("4")){
                        try {
                            viewMovetow.DataZhiXunByChannel(demoData.getKpChannelId(),demoData.getHyChannelId());
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public static void main(String[] args) {





    }

}
