package com.ruoyi.web.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.mall.domain.Bo.VipCodeBO;
import com.ruoyi.mall.domain.VipGuide;
import com.ruoyi.mall.service.IMallConfigService;
import com.ruoyi.mall.service.IMallMemberUserService;
import com.ruoyi.mall.service.IVipGuideService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**会员指南
 **/
@RestController
@RequestMapping("/api/VipGuid")
public class ApiVipGuidController extends BaseController {

    @Resource
    private IVipGuideService vipGuideService;
    @Resource
    private IMallConfigService mallConfigService;
    @Resource
    private RedisCache redisCache;
    @Resource
    private IMallMemberUserService memberUserService;
    /**
     * 会员指南列表
     */
    @GetMapping("/getVipGuidList")
    public AjaxResult getBulletinList(@RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10") Integer pageSize) {
        try{
            PageHelper.startPage(pageNum, pageSize);
            VipGuide vipGuide = new VipGuide();
            List<VipGuide> list = vipGuideService.selectVipGuideList(vipGuide);
            PageInfo<VipGuide> pageInfo = new PageInfo<>(list);
            return AjaxResult.success().put("data",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("会员指南列表查询异常");
        }
    }

    /**
     * 会员权益介绍
     * @return
     */
    @GetMapping("/getVipIntro")
    public AjaxResult getVipIntro() {
        try{
            return AjaxResult.success().put("data",mallConfigService.selectMallConfigByMarK("vip_intro").getContent());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("会员权益介绍查询异常");
        }
    }

    /**
     * 会员优势
     * @return
     */
    @GetMapping("/getVipSuperiority")
    public AjaxResult getVipSuperiority() {
        try{
            return AjaxResult.success().put("data",mallConfigService.selectMallConfigByMarK("vip_superiority").getContent());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("会员优势查询异常");
        }
    }

    /**
     * 会员开通须知
     * @return
     */
    @GetMapping("/getVipNotice")
    public AjaxResult getVipNotice() {
        try{
            return AjaxResult.success().put("data",mallConfigService.selectMallConfigByMarK("vip_notice").getContent());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("会员开通须知查询异常");
        }
    }

    /**
     * 是否显示vip
     * @return
     */
    @GetMapping("/selectVipShow")
    public AjaxResult selectVipShow(){
        try {
            return AjaxResult.success().put("showFlag", vipGuideService.selectVipShow());
        }catch (Exception e){
            return AjaxResult.error("服务器异常");
        }
    }

    /**
     * 使用会员码
     * @param vipCodeBO
     * @param bindingResult
     * @return
     */
    @RepeatSubmit
    @PostMapping("/useVipCode")
    public AjaxResult useVipCode(@RequestBody VipCodeBO vipCodeBO, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return AjaxResult.allParameterErrors(bindingResult);
        }
        try {
            return memberUserService.useVipCode(vipCodeBO);

        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("服务器异常");
        }
    }
}
