package beilv.web.controller.stadium;

import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.common.core.domain.entity.SysDictData;
import beilv.common.core.page.TableDataInfo;
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 org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
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;

    /**
     * 查询赛事发布列表
     */
    @GetMapping("/getCompetitionList")
    @ResponseBody
    public AjaxResult getCompetitionList(Competition competition) {
        startPage();
        competition.setCompetitionState("competiton_state_2");
        List<Competition> list = competitionService.selectCompetitionList(competition);
        return AjaxResult.success(getDataTable(list));
    }


    /**
     * 添加订单
     * 写入订单信息: 下单人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");
        List<Stadium> stadiumList = stadiumService.getStadiumList(stadiumBO);
        stadiumList.forEach(stadium -> {
            stadium.setPaymentStatusLabel(paymentStatus.get(stadium.getPaymentStatus()));
        });
        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);
    }

    /**
     * 查询购票记录
     */
    /*@PostMapping("/getTakePartList")
    @ResponseBody
    public TableDataInfo getTakePartList(Stadium stadium) {
        startPage();
        //通过id获取参赛集合
        List<Stadium> stadiumList = stadiumService.getTakePartList(stadium);
        return getDataTable(stadiumList);
    }
*/
    /**
     * 针对某个购票记录进行退票处理
     *
     * @param orderId 购票记录id
     * @return
     */
    @GetMapping("/close/{orderId}")
    @ResponseBody
    public AjaxResult closeStadiumById(@PathVariable("orderId") String orderId) {
        return toAjax(stadiumService.closeStadiumById(orderId));
    }
}
