package com.nokia.tsl_data.dao;

import com.nokia.tsl_data.entity.SysDataDictionary;
import com.nokia.tsl_data.entity.WorkFlowBasicData;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.sql.Types;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 从 工作流数据库 读取
 */
@Component
public class WorkFlowDao {

    private final JdbcTemplate jdbcTemplate;

    public WorkFlowDao(@Qualifier("workFlowJdbcTemplate") JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    // 获取全部地市信息
    public List<SysDataDictionary> findSysDataDictionaryForCity() {
        return findSysDataDictionaries("city");
    }

    // 获取全部区县信息
    public List<SysDataDictionary> findSysDataDictionaryForRegion() {
        return findSysDataDictionaries("region");
    }

    // 读取某种类型的字典表
    public List<SysDataDictionary> findSysDataDictionaries(String type) {
        String sql = "select id, name, type_code from sys_data_dictionary sdd where type_code = ? ";
        return jdbcTemplate.query(sql,
                (ps) -> {
                    ps.setString(1, type);
                },
                (rs, rowNum) -> {
                    SysDataDictionary sysDataDictionary = new SysDataDictionary();
                    sysDataDictionary.setType("work_flow_" + rs.getString("type_code"));
                    sysDataDictionary.setNickCode(rs.getString("id"));
                    sysDataDictionary.setNickName(rs.getString("name"));
                    return sysDataDictionary;
                });
    }

    // 获取update_time在start之后的全部工单信息
    public List<WorkFlowBasicData> findWorkFlowBasicDataByUpdateTime(Instant start) {
        return findWorkFlowBasicDataByUpdateTime(start, Instant.now());
    }

    // 获取update_time在两个范围之间的全部工单信息
    public List<WorkFlowBasicData> findWorkFlowBasicDataByUpdateTime(Instant start, Instant end) {
        // 20240112 修改 使用 serv_order 替换了kfsn
        String sql = "select order_no, " + //
                "city as city_id, " + //
                "is_effctive as is_effctive, " + //
                "form_basic_data->>'yh_region' as region_id, " + //
                "form_basic_data->>'serv_order' as kfsn, " + //
                "form_basic_data->>'kd_accepttime' as kd_accepttime, " + //
                "form_basic_data->>'kd_requestreplytime' as kd_requestreplytime, " + //
                "form_basic_data->>'re_is_status' as re_is_status_id, " + //
                "form_basic_data->>'kf_file_time' as kf_file_time, " + //
                "form_basic_data->>'kf_file_type' as kf_file_type, " + //
                "create_time as work_flow_create_time,  " + //
                "update_time as work_flow_update_time " + //
                "from flow_form_basic " + //
                "where update_time >= to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss') " + //
                "and update_time <= to_timestamp(?, 'yyyy-mm-dd hh24:mi:ss') ";
        DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                .withZone(ZoneId.of("Asia/Shanghai"));
        Object[] objects = new Object[] { dateFormat.format(start), dateFormat.format(end) };
        int[] argTypes = new int[] { Types.TIMESTAMP, Types.TIMESTAMP };
        return jdbcTemplate.query(sql, objects, argTypes,
                (rs, rowNum) -> {
                    WorkFlowBasicData basicData = new WorkFlowBasicData();
                    basicData.setOrderNo(rs.getString("order_no"));
                    basicData.setIsEffctive(rs.getString("is_effctive"));
                    basicData.setCityId(rs.getString("city_id"));
                    basicData.setRegionId(rs.getString("region_id"));
                    basicData.setKfsn(rs.getString("kfsn"));
                    basicData.setKdAcceptTime(rs.getString("kd_accepttime"));
                    basicData.setKdRequestReplyTime(rs.getString("kd_requestreplytime"));
                    basicData.setReIsStatusId(rs.getString("re_is_status_id"));
                    basicData.setKfFileTime(rs.getString("kf_file_time"));
                    basicData.setKfFileType(rs.getString("kf_file_type"));
                    basicData.setWorkFlowCreateTime(
                            Instant.ofEpochMilli(rs.getTimestamp("work_flow_create_time").getTime()));
                    basicData.setWorkFlowUpdateTime(
                            Instant.ofEpochMilli(rs.getTimestamp("work_flow_update_time").getTime()));
                    return basicData;
                });
    }
}
