package com.jeequan.jeepay.mch.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.MemberAccountHistory;
import com.jeequan.jeepay.db.entity.MemberInfo;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.MemberAccountHistoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 会员账户流水
 *
 * @author xiaoyu
 *
 * @date 2023/4/13 17:22
 */
@RestController
@RequestMapping("/api/member/accountHistory")
public class MemberAccountHistoryController extends CommonCtrl {

    @Autowired private MemberAccountHistoryService accountHistoryService;

    /**
     * @author: xiaoyu
     * @date: 2023/4/13 17:24
     * @describe: 会员账户流水列表
     */
    @PreAuthorize("hasAuthority('ENT_MEMBER_ACCOUNT_HISTORY_LIST')")
    @GetMapping
    public ApiRes list() {
        MemberAccountHistory accountHistory = getObject(MemberAccountHistory.class);

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

        wrapper.eq(MemberAccountHistory::getMchNo, getCurrentMchNo());
        // 查询条件
        accountHistoryService.selectParams(accountHistory, wrapper);

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

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

        wrapper.orderByDesc(MemberAccountHistory::getCreatedAt);

        IPage<MemberAccountHistory> page = accountHistoryService.page(getIPage(), wrapper);
        // 手机号脱敏
        page.getRecords().stream().forEach(i -> i.setMbrTel(DesensitizedUtil.mobilePhone(i.getMbrTel())));
        // 会员头像
        setAvatarUrl(page.getRecords());

        return ApiRes.page(page);
    }

    /**
     * @author: xiaoyu
     * @date: 2023/4/13 17:49
     * @describe: 会员账户流水信息
     */
    @PreAuthorize("hasAuthority('ENT_MEMBER_ACCOUNT_HISTORY_VIEW')")
    @GetMapping("/{hid}")
    public ApiRes detail(@PathVariable("hid") String hid) {

        MemberAccountHistory history = accountHistoryService.getById(hid);

        if (history == null || !history.getMchNo().equals(getCurrentMchNo())) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }

        // 查询会员信息
        MemberInfo memberInfo = memberInfoService.getById(history.getMbrId());
        // 会员头像
        history.addExt("avatarUrl", memberInfo.getAvatarUrl());

        return ApiRes.ok(history);
    }

    /**
     * @author: xiaoyu
     * @date: 2023/4/18 9:32
     * @describe: 账户流水统计
     */
    @RequestMapping(value="/count", method = RequestMethod.GET)
    public ApiRes count() {
        MemberAccountHistory accountHistory = getObject(MemberAccountHistory.class);

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

        wrapper.eq(MemberAccountHistory::getMchNo, getCurrentMchNo());
        // 查询参数
        accountHistoryService.selectParams(accountHistory, wrapper);

        Map count = accountHistoryService.count(wrapper);

        return ApiRes.ok(count);
    }
}
