package com.kym.miniapp.controller;

import com.kym.common.R;
import com.kym.service.ContactService;
import com.kym.service.RechargeConfigService;
import com.kym.service.RechargePromotionService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author skyline
 * @description
 * @date 2023-07-26 23:51
 */
@RestController
@RequestMapping("/common/")
public class CommonController {

    private final ContactService contactService;
    private final RechargeConfigService rechargeConfigService;
    private final RechargePromotionService rechargePromotionService;

    public CommonController(ContactService contactService, RechargeConfigService rechargeConfigService,
                            RechargePromotionService rechargePromotionService) {
        this.contactService = contactService;
        this.rechargeConfigService = rechargeConfigService;
        this.rechargePromotionService = rechargePromotionService;
    }


    /**
     * 联系我们信息
     *
     * @return
     */
    @GetMapping("/contact")
    R<?> contact() {
        return R.success(contactService.list().stream().findFirst());
    }


    /**
     * 充值金额配置项（按站点查询，优先返回站点专属配置，若无则返回默认配置）
     *
     * @return
     */
    @GetMapping("/rechargeConfig")
    R<?> rechargeConfig(@RequestParam(required = false) String stationId) {
        return R.success(rechargeConfigService.listByStationId(stationId));
    }

    /**
     * 查询优惠活动详情（扫码进入时调用）
     */
    @GetMapping("/promotionInfo")
    R<?> promotionInfo(@RequestParam String token) {
        var promotion = rechargePromotionService.getByToken(token);
        if (promotion == null) {
            return R.failed(-1, "优惠活动不存在或已过期");
        }
        return R.success(promotion);
    }
}
