package com.jeequan.jeepay.mgr.ctrl.member;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.MemberRechargeRecord;
import com.jeequan.jeepay.db.entity.PayWay;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MemberRechargeRecordService;
import com.jeequan.jeepay.service.impl.PayWayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员充值记录
 *
 * @author xiaoyu
 * 
 * @date 2023/4/13 17:22
 */
@RestController
@RequestMapping("/api/member/rechargeRecord")
public class MemberRechargeRecordController extends CommonCtrl {

    @Autowired private MemberRechargeRecordService rechargeRecordService;
    @Autowired private PayWayService payWayService;

    /**
     * @author: xiaoyu
     * @date: 2023/4/13 17:24
     * @describe: 会员充值记录列表
     */
    @PreAuthorize("hasAuthority('ENT_MEMBER_RECHARGE_RECORD_LIST')")
    @GetMapping
    public ApiRes list() {
        MemberRechargeRecord rechargeRecord = getObject(MemberRechargeRecord.class);

        LambdaQueryWrapper<MemberRechargeRecord> wrapper = MemberRechargeRecord.gw();

        // 查询参数
        rechargeRecordService.selectParams(rechargeRecord, wrapper);

        wrapper.orderByDesc(MemberRechargeRecord::getCreatedAt);

        IPage<MemberRechargeRecord> page = rechargeRecordService.page(getIPage(), wrapper);

        // 得到所有支付方式
        Map<String, String> payWayNameMap = new HashMap<>();
        List<PayWay> payWayList = payWayService.list();
        // 手机号脱敏、支付方式名称赋值
        page.getRecords().stream().forEach(i -> {
            i.setMbrTel(DesensitizedUtil.mobilePhone(i.getMbrTel()));
            if (!CollectionUtils.isEmpty(payWayList)) {
                for (PayWay payWay:payWayList) {
                    payWayNameMap.put(payWay.getWayCode(), payWay.getWayName());
                }
                // 存入支付方式名称
                if (StringUtils.isNotEmpty(payWayNameMap.get(i.getWayCode()))) {
                    i.addExt("wayName", payWayNameMap.get(i.getWayCode()));
                }else {
                    i.addExt("wayName", i.getWayCode());
                }
            }
        });

        return ApiRes.page(page);
    }

    /**
     * @author: xiaoyu
     * @date: 2023/4/13 17:49
     * @describe: 会员充值记录详情
     */
    @PreAuthorize("hasAuthority('ENT_MEMBER_RECHARGE_RECORD_VIEW')")
    @GetMapping("/{recordId}")
    public ApiRes detail(@PathVariable("recordId") String recordId) {

        MemberRechargeRecord record = rechargeRecordService.getById(recordId);

        return ApiRes.ok(record);
    }

   /**
    * @author: xiaoyu
    * @date: 2023/4/18 9:32
    * @describe: 充值记录统计
    */
    @RequestMapping(value="/count", method = RequestMethod.GET)
    public ApiRes count() {
        MemberRechargeRecord rechargeRecord = getObject(MemberRechargeRecord.class);

        LambdaQueryWrapper<MemberRechargeRecord> wrapper = MemberRechargeRecord.gw();
        // 查询参数
        rechargeRecordService.selectParams(rechargeRecord, wrapper);

        Map count = rechargeRecordService.count(wrapper);
        return ApiRes.ok(count);
    }

}
