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

import cn.hutool.core.util.DesensitizedUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
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.member.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.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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: 会员充值记录列表
     */
    @GetMapping
    public ApiRes list() {
        MemberRechargeRecord rechargeRecord = getObject(MemberRechargeRecord.class);

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

        wrapper.eq(MemberRechargeRecord::getMbrId, getCurrentMbrId());
        // 查询参数
        rechargeRecordService.selectParams(rechargeRecord, wrapper);

        // 额外条件参数
        JSONObject paramJSON = getReqParamJSON();

        // 二合一 会员名称/订单号/手机号
        if (paramJSON != null && StringUtils.isNotEmpty(paramJSON.getString("unionQueryParam"))) {
            wrapper.and(wr -> {
                wr.like(MemberRechargeRecord::getMbrTel, paramJSON.getString("unionQueryParam"))
                        .or().like(MemberRechargeRecord::getMbrName, paramJSON.getString("unionQueryParam"))
                        .or().like(MemberRechargeRecord::getPayOrderId, paramJSON.getString("unionQueryParam"));
            });
        }

        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: 会员充值记录详情
     */
    @GetMapping("/{recordId}")
    public ApiRes detail(@PathVariable("recordId") String recordId) {

        MemberRechargeRecord record = rechargeRecordService.getById(recordId);

        if (record == null || !record.getMbrId().equals(getCurrentMbrId())) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }

        return ApiRes.ok(record);
    }
}
