package beilv.stadium.service.impl;

import beilv.common.utils.uuid.IdUtils;
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;

    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 int toBeStadium(Stadium stadium) {
        //设置订单状态为 待支付
        stadium.setPaymentStatus(DAI_ZHI_FU);
        //设置下单时间为当前时间
        stadium.setRegistrationTime(simpleDateFormat.format(new Date()));
        //生成订单编号
        stadium.setOrderId(IdUtils.fastSimpleUUID());
        return stadiumMapper.toBeStadium(stadium);
    }

    @Override
    public int cancellStadium(Stadium stadium) {
        //设置订单状态为取消
        stadium.setPaymentStatus(YI_QU_XIAO);
        //设置取消时间为当前时间
        stadium.setCancellationTime(simpleDateFormat.format(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(simpleDateFormat.format(new Date()));
        //更新订单信息
        return stadiumMapper.cancellStadium(stadium);
    }

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

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