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


import com.hzs.common.core.constant.msg.FinanceMsgConstants;
import com.hzs.common.core.enums.EOrderPrefix;
import com.hzs.common.core.utils.CommonUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.member.account.service.ICuMemberRechargeService;
import com.hzs.member.base.service.ICuMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;


/**
 * <p>
 * 会员充值表 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-09-08
 */

@RestController
@RequestMapping("/api/recharge")
public class ApiCuMemberRechargeController extends BaseController {

    @Autowired
    ICuMemberRechargeService cuMemberRechargeService;

    @Autowired
    ICuMemberService cuMemberService;


    /**
     * 会员端充值前调用接口（预充值）
     *
     * @return AjaxResult
     * @rechargeAmount 充值金额
     * @memberCode 会员编号
     */
    @GetMapping("/preCharge")
    public AjaxResult precharge(String rechargeAmount, @RequestParam(required = false) String memberCode) {
        AjaxResult ajaxResult = new AjaxResult();
        if (StringUtils.isEmpty(rechargeAmount)) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_AMOUNT_NOT_EMPTY));
        }
        if (new BigDecimal(rechargeAmount).compareTo(BigDecimal.ZERO) < 0) {
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_AMOUNT_NOT_NEGATIVE));
        }
        Integer pkCountry = SecurityUtils.getPkCountry();
        Long pkMember = SecurityUtils.getUserId();
        //充值人
        Long pkCreator = pkMember;
        //为其它人充值
        if (StringUtils.isNotEmpty(memberCode)) {
            CuMember member = cuMemberService.getMember(memberCode);
            if (null == member) {
                return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHAGER_USER_NOT_EXIST));
            }
            //为他人充值时，暂不校验是否伞下
            //输入的会员编号与后台获取的会员不是同一用户时
/*            if (!pkMember.equals(member.getPkId())) {
                //查询当前用户安置伞下
                List<CuMemberExt> cuMemberExtList = cuMemberService.queryPlaceSponsorUnderUmbrella(pkMember);
                Optional<CuMemberExt> optional = cuMemberExtList.stream().filter(m -> m.getMemberCode().equals(memberCode)).findFirst();
                if (!optional.isPresent()) {
                    return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.RECHARGE_MEMBER_UNDER_PLACE));
                }
            }*/
            pkMember = member.getPkId();
        }
        //充值单号
        String rechargeCode = CommonUtil.createSerialNumber(EOrderPrefix.RECHARGE_CODE.getValue());
        boolean result = cuMemberRechargeService.preCharge(rechargeAmount, pkCountry, pkMember, rechargeCode,pkCreator);
        if (result) {
            ajaxResult.put("flag", "1");
            ajaxResult.put("orderCode", rechargeCode);

        } else {
            ajaxResult.put("flag", "2");
            ajaxResult.put("msg", FinanceMsgConstants.RECHAGER_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 校验用户支付密码是否正确
     *
     * @param pwd
     * @return AjaxResult
     */
    @GetMapping("/check-pwd")
    public AjaxResult checkPwd(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            // 用户支付密不能为空
            return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.PAYMENT_PASSWORD_NOT_EMPTY));
        }
        CuMember cuMember = cuMemberService.queryMember(SecurityUtils.getUserId());
        if (SecurityUtils.matchesPassword(pwd, cuMember.getPayPwd())) {
            return AjaxResult.success();
        }
        return AjaxResult.error(TransactionUtils.getContent(FinanceMsgConstants.PAYMENT_PASSWORD_ERROR));
    }

}
