package org.jeecg.modules.newWarning.controller;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.dataVue.service.IMasterService;
import org.jeecg.modules.newWarning.entity.DrillingConditionsLog;
import org.jeecg.modules.newWarning.service.DrillingConditionsLogService;
import org.jeecg.modules.newWarning.service.RealTimeZjService;
import org.jeecg.modules.warning.entity.ConditionItem;
import org.jeecg.modules.warning.entity.Ltd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.util.*;

@RestController
@RequestMapping("/RealTimeZj/")
@Slf4j
public class RealTimeZjController {

    @Autowired
    private RealTimeZjService realTimeZjService;

    @Autowired
    IMasterService iLogicService;
    @Resource
    private DrillingConditionsLogService logService;


    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public Result<List<Ltd>> getList(String jh,String time1,String time2,String templateId){
        Result<List<Ltd>> result=new Result<>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            sdf.parse(time1);
            if(time2!="")
            {
                sdf.parse(time2);
            }
        } catch (ParseException e) {
            log.error(e.getMessage(),e);
            result.error500("时间参数错误");
        }
        Map<String,Object> map=new HashMap<>();
        String aTab = iLogicService.getTableCodeNew(jh);
        aTab.split("@");
        map.put("table",aTab);
        System.out.println(aTab);
        log.info("查询的井表:{}",aTab);
        map.put("jh",jh);
        map.put("timeStart",time1);
        if(time2!="") {
            map.put("timeEnd",time2);
        }
        List<Ltd> list=realTimeZjService.selectList(map);
        log.info("查询的井表结果数据:{}",list);

        if(list.size()>0){
            for (Ltd item:list) {
                String time = item.getTime().substring(0,19);
                item.setTime(time);
            }
            list.get(0).setTemplateId(templateId);
        }else{
            result.setMessage(templateId+","+jh);
        }
        result.setResult(list);
        return result;
    }

    /**
     * 新的获取录井仪的接口
     * @param jh
     * @param type
     * @param templateId
     * @return
     */
    @RequestMapping(value = "/getJwssList", method = RequestMethod.GET)
    public Result<List<Ltd>> getJwssList(String jh,String type,String templateId){
        Result<List<Ltd>> result=new Result<>();
        List<Ltd> list=realTimeZjService.selectJwssList(jh,type);
        if(list.size()>0){
            Collections.sort(list, new Comparator<Ltd>() {
                @Override
                public int compare(Ltd o1, Ltd o2) {
                    return DateUtil.parse(o1.getTime()).compareTo(DateUtil.parse(o2.getTime()));
                }
            });
            list.get(0).setTemplateId(templateId);
        }else{
            result.setMessage(templateId+","+jh);
        }
        result.setResult(list);
        return result;
    }

    @RequestMapping(value = "/getYData", method = RequestMethod.GET)
    public Result<List<Ltd>> getYData(String jh)
    {
        Result<List<Ltd>> result=new Result<>();
        Map<String,Object> map=new HashMap<>();
        map.put("table","Ltd");
        List<Ltd> list=realTimeZjService.selectYData(map);
        result.setResult(list);
        return result;
    }

    @RequestMapping(value = "/getDate", method = RequestMethod.GET)
    public Result<String> getDate(String jh)
    {
        Result<String> result=new Result<>();
        Map<String,Object> map=new HashMap<>();
        String table = iLogicService.getTableCodeNew(jh);
        map.put("table",table);
        Ltd ltd=realTimeZjService.selectDate(map);
        String strDateFormat = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
        String time=sdf.format(ltd.getTime());
        result.setResult(time);
        return result;
    }

    @RequestMapping(value = "/getAllDate", method = RequestMethod.GET)
    public Result<List<String>> getAllDate(String jh)
    {
        Result<List<String>> result=new Result<>();
        Map<String,Object> map=new HashMap<>();
        String table = iLogicService.getTableCodeNew(jh);
        map.put("table",table);
        try {
            List<Map<String,Object>> list=realTimeZjService.selectAllDate(map);
            List<String> resList=new ArrayList<>();
            for (Map<String,Object> item:list)
            {
                resList.add(item.get("TIME").toString());
            }
            List<Ltd> table1 = realTimeZjService.selectNewOne((String) map.get("table"));
            resList.add(JSONArray.fromObject(table1).toString());
            result.setResult(resList);
            return result;
        }catch (Exception e){
            if (e == null) {
                result.error500("未知异常");
                return result;
            }
            String tmp = e.getMessage();
            String flag=null;
            int index = tmp.toUpperCase().indexOf("ORA-");
            if (index != -1) {
                flag= tmp.substring(index + 4, index + 9);
            }
            if("00942".equals(flag)) {
                result.error500("数据库中尚未录入" + jh + "的数据");
            }else{
                result.error500("未知异常");
            }
            return result;
        }
    }

    @RequestMapping(value = "/getMotion", method = RequestMethod.GET)
    public Result<Ltd> getMotion(String jh , String time) {
        Result<Ltd> result=new Result<>();
        String table = iLogicService.getTableCodeNew(jh);
        if (StringUtils.isNotEmpty(time)){
            time += ".000";
        }
        result.setResult(realTimeZjService.selectByTimeAndJh(time,table));
        return result;
    }

    @RequestMapping(value = "/getCondition", method = RequestMethod.GET)
    public Result<List<ConditionItem>> getCondition(String jh , String startTime, String endTime)
    {
        Result<List<ConditionItem>> result=new Result<>();
        String table = iLogicService.getTableCodeNew(jh);

        result.setResult(realTimeZjService.getCondition(startTime,endTime,table));
        return result;
    }

}
