package org.jeecg.modules.dataVue.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.apache.http.conn.socket.LayeredConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.aspectj.weaver.loadtime.Aj;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.sign.util.SslUtils;
import org.jeecg.modules.dataVue.entity.AFocusjh;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.service.*;
import org.jeecg.modules.newWarning.service.RealTimeZjService;
import org.jeecg.modules.warning.entity.Ltd;
import org.jeecg.modules.warning.service.ILtdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.persistence.Id;
import javax.transaction.Transactional;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    RealTimeZjService realTimeZjService;
    @Autowired
    IMasterService iLogicService;
    @Autowired
    ILjrbService iLjrbService;
    @Autowired
    IZjyService iZjyService;
    @Autowired
    IAjhService ajhService;

    @Autowired
    IAFocusjhService focusjhService;

    @Autowired
    ILtdService ltdService;

    @GetMapping(value = "test")
    public void test() {

        String jhS = "牛页1-3HF," +
                "2-1-斜更178," +
                "3-3-斜更120," +
                "3-6-斜更8," +
                "滨斜454," +
                "滨斜697," +
                "渤古斜7," +
                "车古斜40," +
                "东3-平631," +
                "董17," +
                "樊191HF," +
                "樊斜166," +
                "樊页1-1HF," +
                "樊页1-8HF," +
                "樊页2HF," +
                "丰页1HF," +
                "高94-斜12," +
                "高斜86," +
                "孤北古斜4," +
                "孤北古斜6," +
                "孤古斜32," +
                "孤南斜403," +
                "哈山5," +
                "哈山斜4," +
                "奎屯1," +
                "利67-斜18," +
                "利939-斜15," +
                "利斜894," +
                "梁斜114," +
                "罗斜179," +
                "米泉2," +
                "牛55-斜4," +
                "且深1," +
                "青东43," +
                "商25-斜003," +
                "商84-斜55," +
                "胜3-平157," +
                "史152," +
                "通古斜14," +
                "王550-斜2," +
                "王671-斜1," +
                "夏斜947," +
                "义184-斜24," +
                "义193-斜20," +
                "义页1-1VF," +
                "永936-斜6," +
                "永进14," +
                "永进3-平16," +
                "永进303," +
                "永进304," +
                "沾38-斜100," +
                "沾北斜302," +
                "中30-平609," +
                "桩110-斜2," +
                "桩古斜631," +
                "桩海斜271侧," +
                "桩斜191," +
                "庄深1," +
                "坨134-斜3," +
                "坨深斜16," +
                "埕北351-侧5," +
                "埕北古斜19";


        String[] jhs = jhS.split(",");//以逗号分割
        for (String jh : jhs) {
            System.out.println("数据-->>>" + jh);
            String table = iLogicService.getTableCode(jh);
            String sql = " select *" +
                    "  from %s" +
                    " WHERE TIME = (SELECT MAX(TIME) FROM %s)";
            sql = String.format(sql, table, table);
            try {
                List<Map<String, Object>> data = iLtdSourceService.querySql(sql, null);
            } catch (Exception e) {
                System.out.println("错误");
                System.out.println(e);
                System.out.println(jh);

            }

        }


    }


    @GetMapping(value = "test2")
    public void test2() {

        List<String> newTableList = ltdService.selectNewTable();


        String newSQL = "SELECT * " +
                " FROM v_yw_zjrb3 " +
                " WHERE (jh, rq) IN ( " +
                "    SELECT jh, MAX(rq) " +
                "    FROM v_yw_zjrb3 " +
                "    GROUP BY jh " +
                ")  ";

        if (newTableList.size() > 0){
            newSQL += " and jh in ( ";
            for (String jh : newTableList){
                newSQL += "'" + jh + "',";
            }

            newSQL += " '0' ) ";
        }
        List<Map<String, Object>> list = iLjrbService.querySql(newSQL, null);

        List<Ajh> beanList = new ArrayList<>();
        // 查询 录井仪、钻井液 标志
        // TODO: 如果慢的话增加记录表 进行记录
        List<String> wells = new ArrayList<>();
        for (Map<String, Object> row : list) {

            if (row.get("JH") == null) {
                continue;
            }
            String wellName = row.get("JH").toString().trim();

            row.put("JH", wellName);
            wells.add(wellName);

            if (row.get("DSJS")!=null){
                row.put("DSSJ",row.get("DSJS").toString());
            }
            if (row.get("DWMC")!=null){
                row.put("EJDW",row.get("DWMC").toString());
                row.put("SJDW",row.get("DWMC").toString());
            }
            if (row.get("DTMC")!=null){
                row.put("DT",row.get("DTMC").toString());
            }

//            String zjyParam = "zjycs_" + wellName.replace('-', '_');
//            try {
//                String test = String.format("select max(sj) day from %s   ", zjyParam);
//                List<Map<String, Object>> testObj = iZjyService.querySql(test, null);
//                row.put("zjy", 1);
//                row.put("zjyDay", testObj.get(0).get("DAY").toString().split(" ")[0]);
//            } catch (Exception ex) {
//                System.out.println(ex.getMessage());
//                row.put("zjy", 0);
//            }
            Ajh jhRes = BeanUtil.toBean(row, Ajh.class);
            System.out.println(jhRes);
            beanList.add(jhRes);

        }
        updateJhNew(newTableList,beanList);
    }

    @Transactional
    public void updateJhNew(List<String> newTableList , List<Ajh> beanList) {

        List<Ajh> ajhList = ajhService.selectNotNullList();

        List<String> ajhs = ajhList.stream().map(e -> e.getJH()).collect(Collectors.toList());

        List<String> beans = beanList.stream().map(e -> e.getJH()).collect(Collectors.toList());

        List<Ajh> saveList = new ArrayList<>();

        for (String jh : newTableList){
            Ajh ajh;
            if (beans.contains(jh)){
                ajh = beanList.stream().filter(item -> item.getJH().equals(jh)).findFirst().get();
                ajh.setNOTFOUND("0");
            }else {
                ajh = new Ajh();
                ajh.setJH(jh);
                ajh.setEJDW("黄河钻井");
                ajh.setNOTFOUND("1");
            }

            try {
                Map<String, Object> dateMap = new HashMap<>();
                String table = iLogicService.getTableCodeNew(jh);
                dateMap.put("table", table);

                Ltd ltd = realTimeZjService.selectDate(dateMap);

                if (ltd != null && StringUtils.isNotEmpty(ltd.getTime())) {
                    ajh.setLtd(1);
                    ajh.setLtdDay(ltd.getTime().substring(0, 10));
                }


            }catch (Exception e){
                ajh.setLtd(0);
            }

            if (StringUtils.isNotEmpty(jh) && ajhs.contains(jh)){
                try {
                    ajh.setID(ajhList.stream().filter(item -> item.getJH().equals(jh)).findFirst().get().getID());
                    ajhService.updateById(ajh);
                }catch (Exception e){
                    System.out.println(jh);
                    System.out.println(e);
                }
            }else {
                saveList.add(ajh);
            }

        }

        System.out.println("savelist"+ saveList.size());
        ajhService.saveBatch(saveList);
    }

}
