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


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.oConvertUtils;
import jtznyj.common.api.vo.AFocusWarn;
import org.jeecg.modules.dataVue.mapper.AFocusWarnMapper;
import org.jeecg.modules.dataVue.mapper.MockMapper;
import org.jeecg.modules.dataVue.service.IAFocusWarnService;
import org.springframework.stereotype.Service;

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

@Service
public class AFocusWarnServiceImpl extends ServiceImpl<AFocusWarnMapper, AFocusWarn>  implements IAFocusWarnService {
    @Resource
    private MockMapper mockMapper;

    @Override
    public List<Map<String, Object>> querySql(String sqlStr, String reserve) {
        return mockMapper.querySqlA(sqlStr);
    }

    @Override
    public int querySqlCount(String sqlStr, String reserve) {
        return mockMapper.queryCountA(sqlStr);
    }

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

    @Override
    public Map<String,Object> newListWarning(AFocusWarn aFocusWarn,
                                                   Integer pageNo,
                                                   Integer pageSize,
                                                   Integer group,
                                                   String warnTime_s,
                                                   String warnTime_e){

        int start = pageNo * pageSize - pageSize;
        int end = pageNo * pageSize;
        String sql = "";
        String where = " where 0=0 ";
        if (oConvertUtils.isNotEmpty(aFocusWarn.getSource()) && !"全部".equals(aFocusWarn.getSource())){
            where += " and SOURCE = '"+aFocusWarn.getSource() + "' ";
        }
        if (oConvertUtils.isNotEmpty(aFocusWarn.getCatogary())){
            where += " and CATOGARY = '"+aFocusWarn.getCatogary() + "' ";
        }
        if (oConvertUtils.isNotEmpty(warnTime_s)){
            where += " and WARN_TIME > to_date('"+  warnTime_s +" 00:00:00" + "', 'yyyy-mm-dd hh24:mi:ss') ";
        }
        if (oConvertUtils.isNotEmpty(warnTime_e)){
            where += " and WARN_TIME < to_date('"+warnTime_e +" 23:59:59" + "', 'yyyy-mm-dd hh24:mi:ss') ";
        }

        if(oConvertUtils.isNotEmpty(aFocusWarn.getJh())) {
            String[] jhList=aFocusWarn.getJh().split(",");
            String in = " AND JH IN ( ";
            int a = 0;
            for (String jh : jhList){
                a++;
                in += " '"+jh + "' ";
                if (a!=jhList.length){
                    in += ",";
                }
            }
            where +=  in + ")";
        }
        if (group==1){

            sql = "SELECT JH,CATOGARY,COUNT (*) AS COUNT,MAX (WARN_TIME) AS WARN_TIME FROM A_FOCUS_WARN "+ where +" GROUP BY JH,CATOGARY  ORDER BY WARN_TIME desc,JH";
        }else {
            sql = "SELECT * FROM A_FOCUS_WARN "+where+" order by WARN_TIME DESC";
        }
        String pageSql = "select * from (select rownum a,t.* from (" + sql +" ) t where rownum<=" + end + ") where a>" + start;
        String countSql = " select count(*) from ("+sql+")";

        List<Map<String,Object>> list = this.querySql(pageSql,null);
        System.out.println(pageSql);
        /**
         * 没写过oracle的group by 先用代码实现 后期再改
         */
        if (group == 1 ) {
            for (Map<String, Object> map : list) {
                String sqlOne = "SELECT * FROM (SELECT \"NAVICAT_TABLE\".*, ROWNUM \"NAVICAT_ROWNUM\" FROM ( SELECT \"JTZNYJ\".\"A_FOCUS_WARN\".*, ROWID \"NAVICAT_ROWID\" FROM \"JTZNYJ\".\"A_FOCUS_WARN\" WHERE \"JH\" = '" + map.get("JH").toString() +"' AND \"CATOGARY\" = '"+ map.get("CATOGARY").toString() +"' ORDER BY WARN_TIME DESC ) \"NAVICAT_TABLE\" WHERE ROWNUM <= 1) WHERE \"NAVICAT_ROWNUM\" > 0" ;
                List<Map<String,Object>> fcusList = this.querySql(sqlOne,null);
                Map<String,Object> aFocusWarn2 = fcusList.get(0);
                map.put("BJNR", aFocusWarn2.get("BJNR").toString());
                map.put("SOURCE",  aFocusWarn2.get("SOURCE").toString());
                map.put("JUDGE", aFocusWarn2.get("JUDGE"));
                map.put("REMARK", aFocusWarn2.get("REMARK"));
                map.put("RECORDER", aFocusWarn2.get("RECORDER"));
                map.put("UPDATED", aFocusWarn2.get("UPDATED"));
                if (map.get("WARN_TIME")!=null){
                    map.put("WARN_TIME", aFocusWarn2.get("WARN_TIME").toString().replace(".0",""));
                }
            }
        }

        int count = this.querySqlCount(countSql,null);
        Map<String,Object> res = new HashMap<>();
        res.put("records",list);
        res.put("pages",Math.ceil(count / Double.parseDouble(pageSize.toString())));
        res.put("total",count);
        return  res;

    }

}
