package com.yiqi.api.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.BaseEntity;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.RechargeStatusType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.order.domain.OrderRecharge;
import com.yiqi.recharge.domain.RechargePasswordCard;
import com.yiqi.recharge.domain.RechargeType;
import com.yiqi.recharge.domain.dto.weapp.WeAppCardRechargeDTO;
import com.yiqi.recharge.domain.dto.weapp.WeAppRechargeDTO;
import com.yiqi.recharge.service.IRechargePasswordCardService;
import com.yiqi.recharge.service.IRechargeTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "充值套餐")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/rechargeType")
public class WeAppRechargeTypeController extends BaseController {

    @Autowired
    private IRechargeTypeService rechargeTypeService;
    @Autowired
    private IRechargePasswordCardService rechargePasswordCardService;

    @ApiOperation("查询所有充值套餐")
    @GetMapping(value = "all")
    public R<List<RechargeType>> all() throws Exception{
        return R.ok(rechargeTypeService.list(new QueryWrapper<RechargeType>().lambda()
                .eq(RechargeType::getDelFlag, DeleteStatus.OK.getCode())
                .eq(RechargeType::getStatus, StatusType.OK.getCode())
                .orderByDesc(BaseEntity::getCreateTime)));
    }

//    @ApiOperation("充值套餐充值")
//    @PostMapping(value = "recharge")
    public R recharge(@RequestBody @Valid WeAppRechargeDTO weAppRechargeDTO) throws Exception{
        RechargeType rechargeType = rechargeTypeService.getOne(new QueryWrapper<RechargeType>()
                .lambda().eq(RechargeType::getId,weAppRechargeDTO.getRechargeTypeId())
                .eq(RechargeType::getDelFlag,DeleteStatus.OK.getCode())
                .eq(RechargeType::getStatus,StatusType.OK.getCode()),false);
        if(rechargeType == null){
            throw new ServiceException("未查询到充值套餐");
        }

        return R.ok();
    }

//    @Transactional
//    @ApiOperation("密码充值卡充值")
//    @PostMapping(value = "cardRecharge")
    public R cardRecharge(@RequestBody @Valid WeAppCardRechargeDTO weAppCardRechargeDTO) throws Exception{
        RechargePasswordCard rechargePasswordCard = rechargePasswordCardService.getOne(new QueryWrapper<RechargePasswordCard>()
                .lambda().eq(RechargePasswordCard::getCardNo,weAppCardRechargeDTO.getCardNo())
                .eq(RechargePasswordCard::getDelFlag,DeleteStatus.OK.getCode())
                .eq(RechargePasswordCard::getRechargeStatus, RechargeStatusType.NOT_UES.getCode()),false);
        if(rechargePasswordCard == null){
            throw new ServiceException("未查询到充值卡");
        }
        if(!rechargePasswordCard.getPassword().equals(weAppCardRechargeDTO.getPassword())){
            throw new ServiceException("充值卡密码不正确");
        }

        return R.ok();
    }
}
