package org.jeecg.modules.dataVue.service;

import jtznyj.common.api.ICommonApi;
import jtznyj.common.api.vo.AFocusWarn;
import jtznyj.engineer.vo.LtdRecord;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * jeecg-system-local-api 中公用部分
 */
@Service
public class CommonExternalApi implements ICommonApi {
    @Autowired
    IMasterService iLogicService;
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    IZjyService iZjyService;
    @Autowired
    IAFocusWarnService iaFocusWarnService;
    @Autowired
    IAjhService ajhService;


    @Override
    public List<Map<String, Object>> queryDefaultEngineerLogic() {
        return iLogicService.querySql(
                String.format("select  id, create_by, create_time, update_by, update_time,"
                        + " jh, large_text largeText from b_wellownEngineer where jh = '默认' "), null);
    }

    @Override
    public List<Map<String, Object>> queryWellEngineerLogic(String jh) {
        return iLogicService.querySql(
                String.format("select  id, create_by, create_time, update_by, update_time, "
                        + "jh, large_text largeText from b_wellownEngineer where jh ='%s'", jh), null);
    }

    @Override
    public String getTableCode(String jh) {
        return iLogicService.getTableCode(jh);
    }

    @Override
    public List<LtdRecord> queryLtdListInPeriod(String jh, Date end, Integer minutes) {
        String sql = "select * from %s where time >= '%s' and time <= '%s' ORDER BY TIME";

        String aTab = getTableCode(jh);
        sql = String.format(sql, aTab,
                DateFormatUtils.format(DateUtils.addMinutes(end, minutes), "yyyy-MM-dd HH:mm:ss"),
                DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss"));
        return iLtdSourceService.queryLtdRecordList(sql, null);
    }

    @Override
    public List<LtdRecord> queryLtdListInPeriodDesc(String jh, Date end, Integer minutes) {
        String sql = "select * from %s where time >= '%s' and time <= '%s' ORDER BY TIME desc";

        String aTab = getTableCode(jh);
        sql = String.format(sql, aTab,
                DateFormatUtils.format(DateUtils.addMinutes(end, minutes), "yyyy-MM-dd HH:mm:ss"),
                DateFormatUtils.format(end, "yyyy-MM-dd HH:mm:ss"));
        return iLtdSourceService.queryLtdRecordList(sql, null);
    }

    @Override
    public List<Map<String, Object>> queryJhLastDayOnLtd(String jh) {
        String aTab = getTableCode(jh);
        String test = String.format("select max(time) day from %s ", aTab);
        List<Map<String, Object>> testObj = iLtdSourceService.querySql(test, null);
        return testObj;
    }

    @Override
    public List<Map<String, Object>> queryJhLastDayOnZjy(String jh) {
        String table = "zjycs_" + jh.replace('-', '_');
        String test = String.format("select max(sj) day from %s   ", table);
        List<Map<String, Object>> testObj = iZjyService.querySql(test, null);
        return testObj;
    }

    @Override
    public Boolean saveOneAFocusWarn(AFocusWarn src) {
        return iaFocusWarnService.save(src);
    }

    @Override
    public AFocusWarn queryOneAFocusWarnWithSql(String sql) {
        return iaFocusWarnService.queryOneBySql(sql);
    }

    @Override
    public List<Map<String, Object>> queryDepthWithLastTime(String jh) {
        String aTab = iLogicService.getTableCode(jh);
        String ltdsql = " select * from (select depth from %s order by TIME desc) where rownum=1";
        ltdsql = String.format(ltdsql, aTab);
        List<Map<String, Object>> date = iLtdSourceService.querySql(ltdsql, null);
        return date;
    }

    @Override
    public List<Map<String, Object>> queryAllAJh() {
        String sql = " select * from A_JH ";
        List<Map<String, Object>> jhList = new ArrayList<>();
        jhList = ajhService.querySql(sql, null);
        return jhList;
    }

    @Override
    public List<Map<String, Object>> queryWellAfocusWarnLast(String jh) {
        String warnHisSql = "select * from ( select * from A_FOCUS_WARN where JH='%s' and SOURCE='工程预警' order by WARN_TIME desc) where rownum = 1";
        warnHisSql = String.format(warnHisSql, jh);
        List<Map<String, Object>> last = iaFocusWarnService.querySql(warnHisSql, null);
        return last;
    }

}
