package beilv.web.controller.stadium;

import beilv.common.constant.PriceOptions;
import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.common.core.domain.entity.SysDictData;
import beilv.common.core.page.PageDomain;
import beilv.common.core.page.TableDataInfo;
import beilv.common.core.page.TableSupport;
import beilv.common.utils.DictUtils;
import beilv.common.utils.StringUtils;
import beilv.common.utils.uuid.IdUtils;
import beilv.competition.domain.Competition;
import beilv.competition.service.ICompetitionService;
import beilv.stadium.domain.Stadium;
import beilv.stadium.domain.StadiumBO;
import beilv.stadium.service.IStadiumService;
import beilv.system.domain.SysNotice;
import beilv.system.service.ISysConfigService;
import beilv.vipCard.domain.VipCard;
import beilv.vipCard.service.IVipCardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app-api/stadium")
public class StadiumController extends BaseController {

    @Autowired
    private IStadiumService stadiumService;

    @Autowired
    private ICompetitionService competitionService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private IVipCardService vipCardService;

    /**
     * 查询赛事发布列表
     */
    @GetMapping("/getCompetitionList")
    public AjaxResult getCompetitionList(Competition competition) {
        startPage();
        competition.setCompetitionState("competiton_state_2");
        List<Competition> list = competitionService.selectCompetitionList(competition);
        Map<String, String> competitionType = DictUtils.getDictCacheToMap("competition_type");
        Map<String, String> competitionState = DictUtils.getDictCacheToMap("competition_state");

        String vipLevel = null;
        VipCard vipCard = vipCardService.selectVipCardByUserId(competition.getUserId());
        if (ObjectUtils.isNotEmpty(vipCard)) {
            vipLevel = vipCard.getVipLevel();
        }
        //查询会员折扣
        BigDecimal coefficient = BigDecimal.valueOf(Double.parseDouble(StringUtils.isEmpty(vipLevel)? "1" : configService.selectConfigByKey(vipLevel)));
        list.forEach(competition1 -> {
            competition1.setCompetitionPrice(new PriceOptions(competition1.getCompetitionExpense(), coefficient));
            competition1.setViewingPrice(new PriceOptions(competition1.getViewingTicket(), coefficient));
            competition1.setCompetitionTypeLabel(MapUtils.getString(competitionType, competition1.getCompetitionType()));
            competition1.setCompetitionStateLabel(MapUtils.getString(competitionState, competition1.getCompetitionState()));
        });

        return AjaxResult.success(getDataTable(list).getRows());
    }


//    /**
//     * 添加订单
//     * 写入订单信息: 下单人id, 订单编号, 下单时间, 支付状态:待支付
//     * 调用者传递参数: 用户id, 联系人, 联系电话, 赛事id, 订单类型,
//     * return orderId  订单id
//     */
//    @PostMapping("/toBeStadium")
//    public AjaxResult addStadium(@RequestBody Stadium stadium) {
//        String orderId = stadiumService.toBeStadium(stadium);
//        if (StringUtils.isNotEmpty(orderId)) {
//            return AjaxResult.success("下单成功",orderId);
//        } else {
//            return AjaxResult.error("下单失败");
//        }
//    }
//
//    /**
//     * 通过订单id取消订单
//     * 调用者传递参数: 订单id,
//     * 注:  取消订单时在下单后没有支付时, 可以执行的操作. 如果已经支付了, 则需要调用申请退款接口
//     */
//    @PostMapping("/cancellStadium")
//    public AjaxResult cancellStadium(@RequestBody Stadium stadium) {
//        return toAjax(stadiumService.cancellStadium(stadium));
//    }
//
//    /**
//     * 查询订单
//     * 调用者传递参数: 用户id, 订单状态, 订单类型
//     */
//    @GetMapping("/getStadiumList")
//    public AjaxResult getStadiumList(StadiumBO stadiumBO) {
//        startPage();
//        Map<String, String> paymentStatus = DictUtils.getDictCacheToMap("payment_status");
//        Map<String, String> competitionType = DictUtils.getDictCacheToMap("competition_type");
//        List<Stadium> stadiumList = stadiumService.getStadiumList(stadiumBO);
//        stadiumList.forEach(stadium -> {
//            stadium.setPaymentStatusLabel(paymentStatus.get(stadium.getPaymentStatus()));
//            stadium.setCompetitionTypeLabel(competitionType.get(stadium.getCompetitionType()));
//        });
//        return AjaxResult.success(getDataTable(stadiumList));
//    }
//
//    /**
//     * 支付回调
//     * 调用着传递: 订单编号, 支付状态
//     */
//    @PostMapping("/havePaidStadium")
//    public AjaxResult havePaidStadium(@RequestBody Stadium stadium) {
//        return toAjax(stadiumService.havePaidStadium(stadium));
//    }
//
//    /**
//     * 退款回调
//     * 调用者传递参数: 订单id, 用户id
//     */
//    @PostMapping("/refundStadium")
//    public AjaxResult refundStadium(@RequestBody Stadium stadium) {
//        return toAjax(stadiumService.refundStadium(stadium));
//    }
//
//    /**
//     * 支付验证
//     * 支付之前调用方法, 检测是否可以进行支付
//     *
//     * @return Boolean  true: 可以支付; false: 不可以支付
//     */
//    @PostMapping("/paymentVerification")
//    public Boolean paymentVerification(@RequestBody Stadium stadium) {
//        return stadiumService.paymentVerification(stadium);
//    }



}
