package beilv.admissionticket.service.impl;

import beilv.admissionticket.domain.AdmissionTicket;
import beilv.admissionticket.mapper.AdmissionTicketMapper;
import beilv.admissionticket.service.IAdmissionTicketService;
import beilv.common.core.domain.AjaxResult;
import beilv.common.core.text.Convert;
import beilv.common.utils.DateUtils;
import beilv.competition.domain.RedisTask;
import beilv.vipCardLog.domain.VipCardLog;
import beilv.vipCardLog.mapper.VipCardLogMapper;
import beilv.vipCardLog.service.IVipCardLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static beilv.competition.domain.Constant.TASK_QUEUE_APPLY;
import static beilv.competition.domain.Constant.TICKET_QUEUE;

@Service
public class AdmissionTicketServiceImpl implements IAdmissionTicketService {

    @Autowired
    private AdmissionTicketMapper ticketMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private VipCardLogMapper vipCardLogMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IVipCardLogService vipCardLogService;

    @Override
    public int insertAdmissionTicketToList(List<AdmissionTicket> ticketList) {
        return ticketMapper.insertAdmissionTicketToList(ticketList);
    }

    @Autowired
    private AdmissionTicketMapper beilvAdmissionTicketMapper;

    /**
     * 查询约球购票
     *
     * @param id 约球购票主键
     * @return 约球购票
     */
    @Override
    public AdmissionTicket selectBeilvAdmissionTicketById(String id) {
        return beilvAdmissionTicketMapper.selectBeilvAdmissionTicketById(id);
    }

    /**
     * 查询约球购票列表
     *
     * @param AdmissionTicket 约球购票
     * @return 约球购票
     */
    @Override
    public List<AdmissionTicket> selectBeilvAdmissionTicketList(AdmissionTicket AdmissionTicket) {
        return beilvAdmissionTicketMapper.selectBeilvAdmissionTicketList(AdmissionTicket);
    }

    /**
     * 新增约球购票
     *
     * @param AdmissionTicket 约球购票
     * @return 结果
     */
    @Override
    public int insertBeilvAdmissionTicket(AdmissionTicket AdmissionTicket) {
        return beilvAdmissionTicketMapper.insertBeilvAdmissionTicket(AdmissionTicket);
    }

    /**
     * 修改约球购票
     *
     * @param AdmissionTicket 约球购票
     * @return 结果
     */
    @Override
    public int updateBeilvAdmissionTicket(AdmissionTicket AdmissionTicket) {
        return beilvAdmissionTicketMapper.updateBeilvAdmissionTicket(AdmissionTicket);
    }

    /**
     * 批量删除约球购票
     *
     * @param ids 需要删除的约球购票主键
     * @return 结果
     */
    @Override
    public int deleteBeilvAdmissionTicketByIds(String ids) {
        return beilvAdmissionTicketMapper.deleteBeilvAdmissionTicketByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除约球购票信息
     *
     * @param id 约球购票主键
     * @return 结果
     */
    @Override
    public int deleteBeilvAdmissionTicketById(String id) {
        return beilvAdmissionTicketMapper.deleteBeilvAdmissionTicketById(id);
    }

    @Override
    public void updateTicketStatus(List<String> ids, String ticketStatus) {
        beilvAdmissionTicketMapper.updateTicketStatus(ids, ticketStatus);
    }

    @Override
    public Map<String, Object> getThresholdValue(String ticketId) {
        return beilvAdmissionTicketMapper.getThresholdValue(ticketId);
    }

    @Override
    public int confirm(AdmissionTicket beilvAdmissionTicket) {
        //手动核销, 修改订单状态为核销
        VipCardLog vipCardLog = new VipCardLog();
        vipCardLog.setBusId(beilvAdmissionTicket.getId());
        vipCardLog.setOrderType("yueqiu");
        vipCardLog.setVerifyTime(DateUtils.getNowDate());
        vipCardLog.setPaymentStatus("payment_status_verification");
        vipCardLogMapper.updateVipCardLogByBusId(vipCardLog);

        //移除定时任务
        stringRedisTemplate.delete(TICKET_QUEUE+beilvAdmissionTicket.getId());

        //修改场次状态为核销
        return beilvAdmissionTicketMapper.updateBeilvAdmissionTicket(beilvAdmissionTicket);
    }

    @Override
    public AjaxResult refund(AdmissionTicket beilvAdmissionTicket) {
        /*VipCardLog vipCardLog = new VipCardLog();
        vipCardLog.setBusId(beilvAdmissionTicket.getId());
        return vipCardLogService.refundById(vipCardLog, false);*/
        return null;
    }

    @Override
    public void createTask(AdmissionTicket AdmissionTicket){
        AdmissionTicket admissionTicket = beilvAdmissionTicketMapper.selectBeilvAdmissionTicketById(AdmissionTicket.getId());
        Date ticketDate = admissionTicket.getTicketDate(); //2025-11-14
        String session = admissionTicket.getSession().split("-")[0] + ":00"; //10:00:00
        if (session.length() == 7) {
            session = "0" + session; // 将 8:00 补充成 08:00
        }
        // 将日期和时间拼接成一个字符串
        String dateTimeStr = ticketDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate() + " " + session;

        // 使用 DateTimeFormatter 解析字符串为 LocalDateTime 对象
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, formatter);

        // 获取到期时间
        long timestamp = dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        //约场后, 开启定时任务.
        RedisTask redisTaskApply = new RedisTask(admissionTicket.getId(),"4","payment_status_verification");
        redisTemplate.opsForZSet().add(TICKET_QUEUE + admissionTicket.getId(), redisTaskApply.toString(), timestamp/1000);
    }
}
