package org.jeecg.modules.dataVue.task;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.dataVue.entity.AFocusjh;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class jhHistoryOldJob implements Job {

    @Autowired
    ILjrbService iLjrbService;
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    IZjyService iZjyService;
    @Autowired
    IAjhService ajhService;
    @Autowired
    IAFocusjhService focusjhService;

    @Autowired
    IMasterService iLogicService;

    @Value("${server.port}")
    private String port;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        if(!port.equals("8090")){
            return;
        }
        log.info(String.format(" 备份井号的定时任务  时间:" + org.jeecg.common.util.DateUtils.getTimestamp()));
        String sql = " select jh, jh id ,  max(t1.g_cname) ejdw, max(t2.g_cname) sjdw, max(t3.key) dt , max(t4.key) sgyq,  \n" +
                "       max(t.dh) dh , max(t.kzrq) kzrq , max(t.jjrq) 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-1 , 'yyyy/MM/dd')  \n" +
                "                             group by t.jh order by t.jh ";

        iLjrbService.clearEjdwList();

        String newSQL = "select jh, dwmc ejdw, dwmc sjdw, dtmc dt , sgyq,  dh, kzrq, jjrq,sgdd , jfdw,jb,jx from v_yw_zjrb3 where RQ = to_char( SYSDATE , 'yyyy-MM-dd' ) ";


        List<Map<String, Object>> list = iLjrbService.querySql(newSQL, null);



        List<Ajh> beanList=new ArrayList<>();
        // 查询 录井仪、钻井液 标志
        // TODO: 如果慢的话增加记录表 进行记录
        List<String> wells = new ArrayList<>();
        for (Map<String, Object> row : list) {
            if(row.get("JH")!=null){
                String wellName = row.get("JH").toString().trim();
                row.put("JH",wellName);
                wells.add(wellName);
                String aTab = iLogicService.getTableCode(wellName);
                try {
                    String test = String.format("select max(time) day from %s  ", aTab);
                    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);
                }
                Ajh jhRes=  BeanUtil.toBean(row,Ajh.class);
                beanList.add(jhRes);
            }
        }

        updateJh(beanList);

    }


    @Transactional
    public void updateJh(List<Ajh> beanList){
        if (beanList.size()>0){
            QueryWrapper<Ajh> queryWrapper=new QueryWrapper<Ajh>();
            queryWrapper.last(" where 1=1");
            ajhService.remove(queryWrapper);
            ajhService.saveBatch(beanList);

            //删除关注井中没有总井表的数据
            String jhs = "";
            for (Ajh jh : beanList){
                jhs += "'"+ jh.getJH()+"'" + " ,";
            }
            jhs = jhs.substring(0, jhs.length() -1);

            log.info(String.format(" 删除关注井号定时任务  井号:" + jhs));

            QueryWrapper<AFocusjh> aFocusjhQueryWrapper =new QueryWrapper<AFocusjh>();
            aFocusjhQueryWrapper.last(" where jh not in ("+jhs+")" );
            focusjhService.remove(aFocusjhQueryWrapper);
        }
    }
}
