package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiService.VipService;
import org.jeecg.modules.employVip.entity.EmployVip;
import org.jeecg.modules.employVipRecord.entity.EmployVipRecord;
import org.jeecg.modules.employVipType.entity.EmployVipType;
import org.jeecg.modules.employVipType.service.IEmployVipTypeService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.pay.MpWxPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Service
public class VipServiceImpl implements VipService {

    //支付接口
    @Autowired
    private MpWxPayService mpWxPayService;

    //权限验证
    @Resource
    private ShiroRealm shiroRealm;

    //权限验证
    @Resource
    private IEmployVipTypeService employVipTypeService;
    /******************************************************************************************************************/

    @Override
    public Result<?> addVIP(String token, String vipTypeId) {

        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //生成订单信息
        EmployVipRecord vipRecord = new EmployVipRecord();
        String payNo = IdWorker.getIdStr();//生成订单号
        vipRecord.setPayNo(payNo);//订单号
        vipRecord.setUserId(hanHaiMember.getId());//关联用户id
        vipRecord.setVipTypeId(vipTypeId);//会员类型id

        //根据会员类型查询会员名称
        EmployVipType vipType = employVipTypeService
                .lambdaQuery()
                .eq(EmployVipType::getType, vipTypeId)
                .one();

        //吊起微信支付
        BigDecimal price = vipType.getPrice();//价格
        String productName = vipType.getName();//vip类型

        String s = price.multiply(new BigDecimal(100)).toString();
        int i1 = Double.valueOf(s).intValue();
        Object appOrder = mpWxPayService.createOrder(
                "购买"+ productName ,
                "127.0.0.1",
                payNo,
                i1,
                payNo,
                hanHaiMember.getAppletOpenid(),
                vipRecord.toString());

        return Result.OK("支付成功",appOrder);
    }
}
