package co.yixiang.yshop.module.order.service.rechargeorder;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import co.yixiang.yshop.module.member.api.user.MemberUserApi;
import co.yixiang.yshop.module.member.api.user.dto.MemberUserRespDTO;
import co.yixiang.yshop.module.order.controller.admin.rechargeconfig.vo.RechargeConfigExportReqVO;
import co.yixiang.yshop.module.order.controller.admin.rechargeorder.vo.RechargeOrderCreateReqVO;
import co.yixiang.yshop.module.order.controller.app.rechargeorder.vo.AppRechargeOrderCreateReqVO;
import co.yixiang.yshop.module.order.dal.dataobject.rechargeconfig.RechargeConfigDO;
import co.yixiang.yshop.module.order.dal.dataobject.rechargepackage.RechargePackageDO;
import co.yixiang.yshop.module.order.enums.ReachargeOrderStatusEnum;
import co.yixiang.yshop.module.order.service.rechargeconfig.RechargeConfigService;
import co.yixiang.yshop.module.order.service.rechargepackage.RechargePackageService;
import co.yixiang.yshop.module.pay.enums.PayOrderConstants;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static co.yixiang.yshop.module.order.enums.ErrorCodeConstants.RECHARGE_MIN_ERROR;

@Service
public class RechargeFacadeImpl implements RechargeFacade {

    @Resource
    private RechargeOrderService rechargeOrderService;

    @Resource
    private RechargePackageService rechargePackageService;

    @Resource
    private RechargeConfigService rechargeConfigService;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public String create(AppRechargeOrderCreateReqVO createReqVO) {
        // 充值套餐

        Long userId = getLoginUserId();
        MemberUserRespDTO user = memberUserApi.getUser(userId);
        // 获取id工具
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        String nextIdStr = PayOrderConstants.RECHARGE_ORDER_PREFIX + snowflake.nextIdStr();

        // 保存充值订单
        RechargeOrderCreateReqVO rechargeOrderCreateReqVO = new RechargeOrderCreateReqVO();
        rechargeOrderCreateReqVO.setId(nextIdStr);
        rechargeOrderCreateReqVO.setUserId(userId);
        if (createReqVO.getPackageId() != null) {
            RechargePackageDO rechargePackage = rechargePackageService.getRechargePackage(createReqVO.getPackageId());
            rechargeOrderCreateReqVO.setRechargeAmount(rechargePackage.getRechargeAmount());
            rechargeOrderCreateReqVO.setGiftAmount(rechargePackage.getGiftAmount());
            rechargeOrderCreateReqVO.setPackageId(createReqVO.getPackageId());
        } else {
            List<RechargeConfigDO> rechargeConfigList = rechargeConfigService.getRechargeConfigList(new RechargeConfigExportReqVO());
            if (createReqVO.getAmount() < rechargeConfigList.get(0).getCustomMin()) {
                throw exception(RECHARGE_MIN_ERROR);
            }
            rechargeOrderCreateReqVO.setRechargeAmount(createReqVO.getAmount());
            rechargeOrderCreateReqVO.setGiftAmount(0);
        }

        rechargeOrderCreateReqVO.setStatus(ReachargeOrderStatusEnum.WAIT.getValue());
        rechargeOrderService.createRechargeOrder(rechargeOrderCreateReqVO);

        return nextIdStr;
    }
}
