package beilv.stadium.service.impl;

import beilv.common.utils.uuid.IdUtils;
import beilv.competition.domain.Competition;
import beilv.competition.mapper.CompetitionMapper;
import beilv.stadium.domain.Stadium;
import beilv.stadium.domain.StadiumBO;
import beilv.stadium.mapper.StadiumMapper;
import beilv.stadium.service.IStadiumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StadiumServiceImpl implements IStadiumService {

    @Autowired
    private StadiumMapper stadiumMapper;

    @Autowired
    private CompetitionMapper competitionMapper;

    private final static String DAI_ZHI_FU = "payment_status_to_be_paid";
    private final static String YI_ZHI_FU = "payment_status_have_paid";
    private final static String YI_QU_XIAO = "payment_status_cancelled";
    private final static String YI_TUI_KUAN = "payment_status_refunded";

    private final static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public String toBeStadium(Stadium stadium) {
        try{
            String orderId = IdUtils.fastSimpleUUID();
            //设置订单状态为 待支付
            stadium.setPaymentStatus(DAI_ZHI_FU);
            //设置下单时间为当前时间
            stadium.setRegistrationTime(new Date());
            //生成订单编号
            stadium.setOrderId(orderId);
            stadiumMapper.toBeStadium(stadium);
            return orderId;
        }catch (Exception e){
            return "";
        }

    }

    @Override
    public int cancellStadium(Stadium stadium) {
        //设置订单状态为取消
        stadium.setPaymentStatus(YI_QU_XIAO);
        //设置取消时间为当前时间
        stadium.setCancellationTime(new Date());
        return stadiumMapper.cancellStadium(stadium);
    }

    @Override
    public List<Stadium> getStadiumList(StadiumBO stadiumBO) {
        return stadiumMapper.getStadimList(stadiumBO);
    }

    /*@Override
    public int havePaidStadium(Stadium stadium) {
        //设置订单状态为已支付
        stadium.setPaymentStatus(YI_ZHI_FU);
        //设置支付时间
        stadium.setPaymentTime(new Date());

        //增加赛事退单时间验证,如果赛事报名已结束, 则直接设置支付状态为已核销
        *//*Competition competition = competitionMapper.getCompetitionInfo(stadium.getOrderId());
        Date applyStartTime = competition.getApplyStartTime();
        System.out.println("applyStartTime = " + applyStartTime);
        Double applyBeforeTime = competition.getApplyBeforeTime();*//*

        //更新订单信息
        return stadiumMapper.cancellStadium(stadium);
    }*/

    @Override
    public int refundStadium(Stadium stadium) {
        //设置订单状态为取消
        stadium.setPaymentStatus(YI_TUI_KUAN);
        //设置取消时间为当前时间
        stadium.setRefundTime(new Date());
        //修改订单状态为退款
        return stadiumMapper.cancellStadium(stadium);
    }

    /**
     * 根据赛事id查询当前订单是否可以支付
     *
     * @param stadium competitionId 赛事id
     * @return boolean true: 可以支付;  false: 不可以支付
     */
    @Override
    public Boolean paymentVerification(Stadium stadium) {
        return stadiumMapper.paymentVerification(stadium);
    }

    @Override
    public List<Stadium> getTakePartList(Stadium stadium) {
        return stadiumMapper.getTakePartList(stadium);
    }

    @Override
    public int closeStadiumById(String orderId) {
        //发起退票

        //修改购票状态
        Stadium stadium = new Stadium();
        stadium.setOrderId(orderId);
        stadium.setRefundInstructions("商家发起退款");
        //设置订单状态
        stadium.setPaymentStatus("payment_status_refunded");
        //设置退款时间
        stadium.setRefundTime(new Date());
        return stadiumMapper.cancellStadium(stadium);
    }
}
