package org.jeecg.modules.warning.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.warning.entity.Ltd;
import org.jeecg.modules.warning.entity.Zjycs;
import org.jeecg.modules.warning.entity.ZjycsLtd;
import org.jeecg.modules.warning.model.DrillInfoModel;
import org.jeecg.modules.warning.service.IFzqkService;
import org.jeecg.modules.warning.service.IZjycsService;
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 java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/warning")
@Slf4j
public class WarningController {
    @Autowired
    private IZjycsService iZjycsService;
    @Autowired
    private IFzqkService iFzqkService;

    /**
     * 钻井液预警
     *
     * @param jh
     * @param time
     * @return
     */
    @RequestMapping(value = "/zjycs/getList", method = RequestMethod.GET)
    public Result<List<Zjycs>> getList(String jh, String time) {
        Result<List<Zjycs>> result = new Result<>();
        String tableName = "ZJYCS_" + jh.replace("-", "_") + "_BJ";
        Map<String, Object> map = new HashMap<>();
        map.put("table", tableName);
        map.put("time", time);
        List<Zjycs> list = iZjycsService.selectList(map);
        if (list.size() > 0) {
            list.get(0).setJh(jh);
        }
        result.setResult(list);
        return result;
    }

    /**
     * 钻井液录井仪数据
     * @param jh
     * @param time1
     * @param time2
     * @return
     */
    @RequestMapping(value = "/zjycs/getLtd", method = RequestMethod.GET)
    public Result<List<ZjycsLtd>> getLtd(String jh,String time1,String time2){
        Result<List<ZjycsLtd>> 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 table="ZJYCS_"+jh.replace("-","_");
        map.put("table",table);
        map.put("timeStart",time1);
        if(time2!="")
        {
            map.put("timeEnd",time2);
        }
        List<ZjycsLtd> list=iZjycsService.selectLtd(map);
        result.setResult(list);
        return result;
    }

    /**
     * 钻井液时间参数
     * @param jh
     * @return
     */
    @RequestMapping(value = "/zjycs/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="ZJYCS_"+jh.replace("-","_");
        map.put("table",table);
        try {
            List<Map<String,Object>> list=iZjycsService.selectAllDate(map);
            List<String> resList=new ArrayList<>();
            for (Map<String,Object> item:list)
            {
                resList.add(item.get("TIME").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(flag.equals("00942"))
            {
                result.error500("数据库中尚未录入" + jh + "的数据");
            }else{
                result.error500("未知异常");
            }
            return result;
        }
    }
    /**
     * 复杂情况预警暂时测试用
     *
     * @param radius
     * @param jh
     * @param jsStart
     * @param jsEnd
     * @param dataType
     * @returnZhrui@123456
     */
    @RequestMapping(value = "/fzqk/Calculate", method = RequestMethod.GET)
    public Result<DrillInfoModel> Calculate(double radius, String jh, String jsStart, String jsEnd,double x,double y, int dataType) {
        Result<DrillInfoModel> result = new Result<>();
        Map<String, List<DrillInfoModel>> calDataList = iFzqkService.Calculate(radius, jh, jsStart, jsEnd, dataType, -1, -1);
        DrillInfoModel demoModel = new DrillInfoModel();
        demoModel.setCorr(0);
        calDataList.forEach((s, list) -> {
            list.forEach(d -> {
                if (Double.compare(d.getCorr(), demoModel.getCorr()) > 0) {
                    try {
                        BeanUtils.copyProperties(demoModel, d);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            });
        });
        result.setResult(demoModel);
        return result;
    }
}
