package com.hzs.member.account.controller.api;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.enums.ETradeType;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.domain.EnumEntity;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.account.service.ICuMemberAccountService;
import com.hzs.member.account.service.ICuMemberTradeService;
import com.hzs.member.account.vo.CuMemberTradeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的钱包
 */
@RestController
@RequestMapping("/api/wallet")
public class ApiCuMemberWalletController extends BaseController {

    @Autowired
    private ICuMemberAccountService iCuMemberAccountService;
    @Autowired
    private ICuMemberTradeService iCuMemberTradeService;


    /**
     * 查询钱包余额
     *
     * @return
     */
    @GetMapping("/balance")
    public AjaxResult getWalletBalance() {
        Long pkMember = SecurityUtils.getUserId();
        Integer pkCountry = SecurityUtils.getPkCountry();
        return AjaxResult.success(iCuMemberAccountService.queryWalletBalanceByPkMember(pkMember, pkCountry));
    }


    /**
     * 查询交易类型
     *
     * @return
     */
    @GetMapping("/trade-type")
    public AjaxResult tradeType() {
        // 查询交易类型
        List<Integer> tradeTypes = iCuMemberTradeService.selectTradeType(SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
        List<EnumEntity> enumEntityList = new ArrayList<>();
        for (ETradeType value : ETradeType.values()) {
            if (tradeTypes.contains(value.getValue())) {
                enumEntityList.add(new EnumEntity(value.getValue(), value.getLabel()));
            }
        }
        return AjaxResult.success(enumEntityList);
    }


    /**
     * 查询交易明细
     *
     * @param cuMemberTradeVO 交易记录表
     * @return TableDataInfo
     */
    @PostMapping("/transaction-details")
    public TableDataInfo selectWattleDetail(@RequestBody CuMemberTradeVO cuMemberTradeVO) {
        cuMemberTradeVO.setPkCountry(SecurityUtils.getPkCountry());
        cuMemberTradeVO.setPkMember(SecurityUtils.getUserId());
        startPage();
        List<CuMemberTradeVO> cuMemberTradeVOList = iCuMemberTradeService.selectWattleDetail(cuMemberTradeVO);
        if (CollectionUtil.isNotEmpty(cuMemberTradeVOList)) {
            for (CuMemberTradeVO memberTradeVO : cuMemberTradeVOList) {
                memberTradeVO.setTradeTypeVal(ETradeType.getTradeLabel(memberTradeVO.getTradeType()));
            }
        }
        return getDataTable(cuMemberTradeVOList);
    }
}
