package com.yiqi.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiqi.app.domain.AppUserBillDetail;
import com.yiqi.app.domain.AppUserBillMstr;
import com.yiqi.app.domain.dto.weapp.WeAppUserBillDetailQueryByCardDTO;
import com.yiqi.app.service.IAppUserBillDetailService;
import com.yiqi.app.service.IAppUserBillMstrService;
import com.yiqi.common.config.RuoYiConfig;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.BaseEntity;
import com.yiqi.common.core.domain.PageVO;
import com.yiqi.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

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

@Api(tags = "用户账单")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/bill/mstr")
public class WeAppBillMstrController extends BaseController {

    @Autowired
    private IAppUserBillMstrService appUserBillMstrService;

    @Autowired
    private IAppUserBillDetailService appUserBillDetailService;

    @ApiOperation("根据充值卡查询账单记录")
    @PostMapping(value = "findPageByCard")
    public R<List<AppUserBillDetail>> findPageByCard(@RequestBody @Valid WeAppUserBillDetailQueryByCardDTO weAppUserBillDetailQueryByCardDTO) throws Exception{
        return R.ok(appUserBillDetailService.page(new Page<>(weAppUserBillDetailQueryByCardDTO.getPageNum()
                        , weAppUserBillDetailQueryByCardDTO.getPageSize())
                ,new QueryWrapper<AppUserBillDetail>().lambda()
                        .eq(AppUserBillDetail::getRechargeCardId,weAppUserBillDetailQueryByCardDTO.getRechargeCardId())
                        .eq(AppUserBillDetail::getAppUserId, AuthHolder.userId())
                        .orderByDesc(BaseEntity::getCreateTime)).getRecords());
    }

    @ApiOperation("查询积分使用情况")
    @PostMapping(value = "findPointPage")
    public R<List<AppUserBillMstr>> findPointPage(@RequestBody @Valid PageVO pageVO) throws Exception{
        LambdaQueryWrapper<AppUserBillMstr> queryWrapper = new QueryWrapper<AppUserBillMstr>().lambda()
                .eq(AppUserBillMstr::getAppUserId,AuthHolder.userId())
                .and(appUserBillMstrLambdaQueryWrapper
                        -> appUserBillMstrLambdaQueryWrapper.gt(AppUserBillMstr::getReceivePoint,0).or().gt(AppUserBillMstr::getUsePoint,0))
                .orderByDesc(BaseEntity::getCreateTime);
        List<AppUserBillMstr> list = appUserBillMstrService.page(new Page<AppUserBillMstr>(
                pageVO.getPageNum(),
                pageVO.getPageSize(),
                false),
                queryWrapper).getRecords();
        return R.ok(list);
    }
}
