"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatchTimes = exports.Issues = exports.Issue = void 0;
const Core = require("../table");
const Model = require("../model");
const moment = require("moment");
const _ = require("lodash");
const config = require("../../../../config");
const cache_1 = require("../../../cache");
async function Issue(issueId) {
    let t = await Model.Issue.findOne({
        where: {
            Id: issueId
        }
    });
    return t;
}
exports.Issue = Issue;
;
const Issues = async (root, args, context, isUpdateCache = false) => {
    let lotId = 72;
    if (parseInt(args.lotId) > 0) {
        lotId = parseInt(args.lotId);
    }
    // console.info(args);
    return (0, cache_1.cache)(async ({ lotId, limit }) => {
        let sql = `select * from core_issue where LotteryId=${lotId} 
  and  (EndTime<'${moment().add(3, `hour`).format(`YYYY-MM-DD HH:mm:ss`)}')
  order by Name desc limit ${args.limit || 30}
  `;
        if ([45, 73, 72, 74, 75].includes(lotId) && (args.is_end == null || args.is_end == 1)) {
            let old_match = await Core.Match.findOne({
                where: {
                    //MatchTime:{$lt:`${moment().add(-118,"minute").format(`YYYY-MM-DD HH:mm:ss`)}`},
                    //IssueName:`${moment().format(`YYYY-MM-DD HH:mm:ss`)}`,
                    LotteryId: lotId,
                    MatchState: '完',
                },
                limit: 1,
                order: [["MatchTime", "desc"]]
            });
            if (old_match != null) {
                if (lotId == 72 && old_match.IssueName != moment().format(`YYYY-MM-DD`)) {
                    sql = `select * from core_issue where LotteryId=${lotId} 
  and  (EndTime<'${moment().add(3, `hour`).format(`YYYY-MM-DD HH:mm:ss`)}')
  order by Name desc limit ${args.limit || 30}
  `;
                }
                else {
                    sql = `select * from core_issue where LotteryId=${lotId} 
      and Name<='${old_match.IssueName}'
      order by StartTime desc limit  ${args.limit || 30}
      `;
                }
            }
        }
        else if ([45, 72, 73, 74, 75].includes(lotId)) {
            // 1 完场的期号 2 是未完场的期号 3不限制是否完场 0 代表当前期号
            switch (args.is_end) {
                case 2:
                    sql = `select * from core_issue where LotteryId=${lotId} 
          and  EndTime>'${moment().format(`YYYY-MM-DD HH:mm`)}'
          order by Name desc limit  ${args.limit || 30}
          `;
                    break;
                case 3:
                    sql = `select * from core_issue where LotteryId=${lotId} 
          and  StartTime>'${moment().add(-100, `day`).format(`YYYY-MM-DD HH:mm:ss`)}'
          order by Name desc limit  ${args.limit || 30}
          `;
                    break;
                case 0:
                    sql = `select * from core_issue where LotteryId=${lotId} 
            and  StartTime>'${moment().add(-3, `hour`).format(`YYYY-MM-DD HH:mm:ss`)}'
            order by Name asc limit 1
            `;
                    break;
            }
        }
        // console.info(sql);
        let t = await Core.sequelize.query(sql);
        t = checkList(t);
        return t;
    }, { lotId, is_end: args.is_end }, 5, "pginfo_issue:vy5", 60, 100);
};
exports.Issues = Issues;
const MatchTimes = async (root, args, context, isUpdateCache = false) => {
    let lotId = 72;
    if (parseInt(args.lotId) > 0) {
        lotId = parseInt(args.lotId);
    }
    let key = `${config.CachePrefix}match:matchMatchDates:v1_${args.lotId}`;
    let reply = await cache_1.redis.get(key);
    try {
        if (reply && reply.length > 0 && !isUpdateCache) {
            return JSON.parse(reply);
        }
    }
    catch (e) {
        console.info(e);
    }
    let sql = `
  select distinct to_char("MatchTime",'YYYY-MM-DD') as "MatchTime"
from core_match
where "LotteryId"=${lotId}  and "MatchTime">'${moment().add(-20, 'day').format('YYYY-MM-DD')}'
ORDER BY "MatchTime" desc limit 13
  `;
    let t = await Core.sequelize.query(sql);
    t = checkList(t);
    t = _.map(t, o => o.MatchTime);
    await cache_1.redis.set(key, JSON.stringify(t));
    cache_1.redis.expire(key, 60);
    return t;
};
exports.MatchTimes = MatchTimes;
function checkList(list) {
    if (list && list.length > 0) {
        return list[0];
    }
    return [];
}
//# sourceMappingURL=issue.js.map