package beilv.web.controller.carinformation;

import beilv.cardpurchaserecord.domain.CardPurchaseRecord;
import beilv.cardpurchaserecord.service.ICardPurchaseRecordService;
import beilv.carinformation.domain.CarInformation;
import beilv.carinformation.service.ICarInformationService;
import beilv.common.core.controller.BaseController;
import beilv.common.core.domain.AjaxResult;
import beilv.usermembershipcard.domain.UserMembershipCard;
import beilv.usermembershipcard.service.IUserMembershipCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 会员卡AppController
 *
 * @author ruoyi
 * @date 2025-01-02
 */
@Controller
@RequestMapping("/app-api/cardApp")
public class cardAppController extends BaseController {

    @Autowired
    private ICarInformationService carInformationService;

    @Autowired
    private ICardPurchaseRecordService cardPurchaseRecordService;

    @Autowired
    private IUserMembershipCardService userMembershipCardService;
    /**
     * 查询卡种信息列表
     */
    @PostMapping("/cardInformationlist")
    @ResponseBody
    public AjaxResult cardInformationlist(@RequestBody CarInformation carInformation) {
        return AjaxResult.success(carInformationService.selectCarInformationList(carInformation));
    }

    /**
     * 查询购卡记录列表
     */
    @PostMapping("/getRecordlist")
    @ResponseBody
    public AjaxResult list(@RequestBody CardPurchaseRecord cardPurchaseRecord) {
        List<CardPurchaseRecord> list = cardPurchaseRecordService.selectCardPurchaseRecordList(cardPurchaseRecord);
        return AjaxResult.success(list);
    }

    /**
     * 购卡生成订单
     */
    @PostMapping("/addCard")
    @ResponseBody
    public AjaxResult addCard(@RequestBody CardPurchaseRecord cardPurchaseRecord) {
        return AjaxResult.success(cardPurchaseRecordService.insertCardPurchaseRecord(cardPurchaseRecord));
    }

    /**
     * 购卡回调（购卡、取消订单）
     */
    @PostMapping("/addCardCallback")
    @ResponseBody
    public AjaxResult addCardCallback(@RequestBody CardPurchaseRecord cardPurchaseRecord) {
        return toAjax(cardPurchaseRecordService.addCardCallback(cardPurchaseRecord));
    }

    /**
     * 查询用户会员卡列表
     */
    @PostMapping("/getUserCardList")
    @ResponseBody
    public AjaxResult getUserCardList(@RequestBody UserMembershipCard userMembershipCard) {
        return AjaxResult.success(userMembershipCardService.selectUserMembershipCardList(userMembershipCard));
    }

    /*
     * 退卡
     *
     * @author 韩福成
     * @date 2025/1/7 上午10:46
     */
    @PostMapping("/refundCard")
    @ResponseBody
    public AjaxResult refundCard(@RequestBody UserMembershipCard userMembershipCard) {
        int inserted = userMembershipCardService.refundCard(userMembershipCard);
        if (inserted == -1){
            return AjaxResult.error("退款金额小于0，不支持退款！");
        }
        return toAjax(inserted);
    }
}
