package com.yiqi.api.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.PageVO;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.enums.RechargeStatusType;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.recharge.domain.RechargePasswordCard;
import com.yiqi.recharge.domain.vo.RechargePasswordCardQueryVO;
import com.yiqi.recharge.domain.vo.weapp.WeAppRechargePasswordCardVO;
import com.yiqi.recharge.service.IRechargePasswordCardService;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.service.ISysOrgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author xiaohei
 * @date 2023/10/30
 */
@Api(tags = "充值卡")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/recharge/card")
public class WeAppRechargePasswordCardController {

    @Autowired
    private IRechargePasswordCardService rechargePasswordCardService;
    @Autowired
    private ISysOrgService sysOrgService;

    @ApiOperation("分页获取我的充值卡列表")
    @PostMapping(value = "findPage")
    public R<List<RechargePasswordCardQueryVO>> findPage(@RequestBody @Valid PageVO pageVO) throws Exception{
        return R.ok(rechargePasswordCardService.selectRechargePasswordCardList(pageVO));
    }

    @ApiOperation("根据密码卡编号查询密码卡信息")
    @GetMapping(value = "getinfo")
    public R<WeAppRechargePasswordCardVO> getinfo(@RequestParam @ApiParam("卡号") String cardNo) throws Exception{
        RechargePasswordCard rechargePasswordCard = rechargePasswordCardService.getOne(new QueryWrapper<RechargePasswordCard>()
                .lambda().eq(RechargePasswordCard::getCardNo,cardNo)
                .eq(RechargePasswordCard::getDelFlag,DeleteStatus.OK.getCode())
                .eq(RechargePasswordCard::getRechargeStatus,RechargeStatusType.NOT_UES.getCode()),false);
        if(rechargePasswordCard == null){
            throw new ServiceException("未查询到密码卡");
        }
        WeAppRechargePasswordCardVO weAppRechargePasswordCardVO = new WeAppRechargePasswordCardVO();
        BeanUtils.copyProperties(rechargePasswordCard,weAppRechargePasswordCardVO);

        SysOrg sysOrg = sysOrgService.getOne(new QueryWrapper<SysOrg>().lambda()
                .eq(SysOrg::getStatus, StatusType.OK.getCode())
                .eq(SysOrg::getId,rechargePasswordCard.getRechargeStoreId()));
        weAppRechargePasswordCardVO.setSysOrg(sysOrg);
        return R.ok(weAppRechargePasswordCardVO);
    }
}
