package org.jeecg.modules.dataVue.service.impl;

import org.apache.commons.lang.StringUtils;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.service.*;
import org.jeecg.modules.warning.entity.Ltd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LtdAndJhServiceImpl implements ILtdAndJhService {

    @Autowired
    ILjrbService iLjrbService;
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    IZjyService iZjyService;

    @Autowired
    IMasterService iLogicService;

    @Resource
    private IAjhService ajhService;

    /**
     *
     * @param ejdw
     * @param jh
     * @param pageNo
     * @param pageSize
     * @param jwss
     * @return
     */
    //@Cacheable(cacheNames="ltdandjh",key = "#ejdw+#jh+#pageNo+#pageSize+#jwss")
    @Override
    public Map<String, Object> getDrillingPageWell(String ejdw, String jh, Integer pageNo, Integer pageSize,String jwss) {
        String sql = " select jh, jh id , \n t1.g_cname ejdw, t2.g_cname sjdw, t3.key dt , t4.key sgyq, \n" +
                "        t.dh, t.kzrq, t.jjrq from v_yw_zjrb t \n" +
                "  left join v_sys_group t1 on t1.groupid = t.ejdw\n" +
                "  left join v_sys_group t2 on t2.groupid = t.sjdw\n" +
                "  left join v_sys_constant t3 on t3.id = t.dt\n" +
                "  left join v_sys_constant t4 on t4.id = t.sgyq" +
                "                             where to_char(t.lrsj, 'yyyy/MM/dd') = to_char(sysdate, 'yyyy/MM/dd') \n" +
                "                               and jh like '%%%s%%'\n" +
                (StringUtils.isNotEmpty(ejdw) ? String.format(" and t.ejdw = %s ", ejdw) : "") +
                "                             order by t.jh ";
        String epbpSql = "\n" +
                "select tt1.*, tt3.kzrq, tt2.ssdw, tt2.gldw, tt3.sjjs\n" +
                "  from (select *\n" +
                "          from (select row_number() over(partition by jh order by rq desc) pn,\n" +
                "                       zjdh,\n" +
                "                       jh,\n" +
                "                       zjrzgznr scjk,\n" +
                "                       zjdt,\n" +
                "                       rq lrsj\n" +
                "                  from \"JTSJZXYYK-EPBW\".ZJ_ZJDT)\n" +
                "         where pn = 1\n" +
                "           and to_char(lrsj, 'yyyy/MM/dd') =\n" +
                "               to_char(sysdate - 1, 'yyyy/MM/dd')) tt1\n" +
                "  left join (select jh, kzrq, rjc, sjjs\n" +
                "               from \"JTSJZXYYK-EPBW\".ZJ_ZJSCDT\n" +
                "              where (jh, sj) in (select jh, max(sj)\n" +
                "                                   from \"JTSJZXYYK-EPBW\".ZJ_ZJSCDT t\n" +
                "                                  group by jh)) tt3\n" +
                "    on tt3.jh = tt1.jh\n" +
                "  left join \"JTSJZXYYK-EPBW\".epbp_zj_djjc tt2\n" +
                "    on tt1. jh = tt2.jh\n" +
                " order by tt1.jh ";
        sql = String.format(sql, jh);
        int start = pageNo * pageSize - pageSize;
        int end = pageNo * pageSize;
        String condition = "";
        String pageSql = "select * from (select rownum a,t.* from (" + sql + " " + condition + ") t where rownum<=" + end + ") where a>" + start;
        String countSql = " select count(*) from (" + sql.toString() + " ）" + condition;

        int count = iLjrbService.querySqlCount(countSql, null);// iLjrbService.getJdbc().queryForObject(countSql, int.class);
        List<Map<String, Object>> list = iLjrbService.querySql(pageSql, null);

        // 查询 录井仪、钻井液 标志
        // TODO: 如果慢的话增加记录表 进行记录
        List<String> wells = new ArrayList<>();
        if(jwss.equals("Y")){
            for (Map<String, Object> row : list) {
                String wellName = row.get("JH").toString();
                try {
                    List<Ltd> jwssList=iLtdSourceService.selectJwssList(wellName,"");
                    if(jwssList.size()>0){
                        row.put("ltd", 1);
                        row.put("ltdDay", jwssList.get(0).getTime());
                    }else{
                        row.put("ltd", 0);
                    }

                } catch (Exception ex) {
                    System.out.println(ex.getMessage());
                    row.put("ltd", 0);
                }
                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);
                }
            }
        }else {
            for (Map<String, Object> row : list) {
                String wellName = row.get("JH").toString();
                row.put("ltd", 0);
                Ajh aJh = ajhService.selectAjhByJh(wellName);
                if (aJh != null){
                    row.put("ltd", 1);
                }
//                wells.add(wellName);
//                String table = iLogicService.getTableCode(wellName);
//                try {
//                    String test = String.format("select max(time) day from %s ", table);
//                    List<Map<String, Object>> testObj = iLtdSourceService.querySql(test, null);
//                    row.put("ltd", 1);
//                    row.put("ltdDay", testObj.get(0).get("DAY").toString().split(" ")[0]);
//                } catch (Exception ex) {
//                    System.out.println(ex.getMessage());
//                    row.put("ltd", 0);
//                }
                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);
                }
            }
        }


        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("count", count);
        return result;
    }
}
