package org.jeecg.modules.dataVue.task;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.entity.AjhJwss;
import org.jeecg.modules.dataVue.service.*;
import org.jeecg.modules.warning.entity.Ltd;
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;

public class jhJwssHistoryJob implements Job {

    @Autowired
    ILjrbService iLjrbService;
    @Autowired
    ILtdSourceService iLtdSourceService;
    @Autowired
    IZjyService iZjyService;
    @Autowired
    IAjhJwssService ajhJwssService;
    @Value("${server.port}")
    private String port;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        if (!port.equals("8090")) {
            return;
        }

        String sql = " select jh, jh id , \n t1.g_cname ejdw, t2.g_cname sjdw, t3.key dt , t4.key sgyq, \n" +
                "        t.dh, t.kzrq, t.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, 'yyyy/MM/dd') \n" +
                "                             order by t.jh ";


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

        List<AjhJwss> beanList=new ArrayList<>();
        // 查询 录井仪、钻井液 标志
        // TODO: 如果慢的话增加记录表 进行记录
        for (Map<String, Object> row : list) {
            if(row.get("JH")!=null){
                String wellName = row.get("JH").toString();
                try {
                    List<Ltd> jwssList=iLtdSourceService.selectJwssList(wellName,"");
                    if(jwssList.size()>0){
                        row.put("ltd", 1);
                        row.put("ltdDay", jwssList.get(0).getTime());
                    }else{
                        row.put("ltd", 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);
                }
                AjhJwss jhRes=  BeanUtil.toBean(row,AjhJwss.class);
                beanList.add(jhRes);
            }

        }

        updateJh(beanList);

    }

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