package cn.newfeifan.mall.module.distri.service.usertopupconsumptionpointsorder;

import cn.newfeifan.mall.module.distri.constant.DistriConstants;
import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
import cn.newfeifan.mall.module.distri.dal.redis.no.OrderNoRedisDAO;
import cn.newfeifan.mall.module.distri.service.orderpercentage.OrderPercentageService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import cn.newfeifan.mall.module.distri.controller.app.usertopupconsumptionpointsorder.vo.*;
import cn.newfeifan.mall.module.distri.dal.dataobject.usertopupconsumptionpointsorder.UserTopUpConsumptionPointsOrderDO;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;

import cn.newfeifan.mall.module.distri.dal.mysql.usertopupconsumptionpointsorder.UserTopUpConsumptionPointsOrderMapper;


import static cn.newfeifan.mall.framework.common.util.servlet.ServletUtils.getClientIP;
import static cn.newfeifan.mall.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.newfeifan.mall.framework.web.core.util.WebFrameworkUtils.getTerminal;

/**
 * 用户充值消费分订单 Service 实现类
 *
 * @author 非繁人
 */
@Service
@Validated
@Slf4j
public class UserTopUpConsumptionPointsOrderServiceImpl implements UserTopUpConsumptionPointsOrderService {

    @Resource
    private UserTopUpConsumptionPointsOrderMapper userTopUpConsumptionPointsOrderMapper;
    @Resource
    private OrderPercentageService orderPercentageService;
    @Resource
    private OrderNoRedisDAO orderNoRedisDAO;

    @Override
    public UserTopUpConsumptionPointsOrderInfoRespVO createUserTopUpConsumptionPointsOrder(UserTopUpConsumptionPointsOrderSaveReqVO createReqVO) {
        // 初始化订单
        orderInit(createReqVO);

        UserTopUpConsumptionPointsOrderDO userTopUpConsumptionPointsOrder = BeanUtils.toBean(createReqVO, UserTopUpConsumptionPointsOrderDO.class);
        userTopUpConsumptionPointsOrderMapper.insert(userTopUpConsumptionPointsOrder);


        // 同步支付订单id
        userTopUpConsumptionPointsOrderMapper.updateById(userTopUpConsumptionPointsOrder);

        // 返回
        return UserTopUpConsumptionPointsOrderInfoRespVO.builder()
                .id(userTopUpConsumptionPointsOrder.getId())
                .payOrderId(userTopUpConsumptionPointsOrder.getPayOrderId())
                .build();
    }


    /**
     * 订单转换
     *
     * @param createReqVO       订单信息
     */
    private void orderInit(UserTopUpConsumptionPointsOrderSaveReqVO createReqVO) {
        // 计算百分比
        OrderPercentageDO orderPercentageDO = orderPercentageService.queryStatus();

//        if(createReqVO.getPayPrice() < Double.parseDouble(orderPercentageDO.getUserTopUpConsumptionPoints())){
//            ErrorCode ERROR = new ErrorCode(1_002_030_038, "用户充值金额不得小于:" + orderPercentageDO.getUserTopUpConsumptionPoints());
//            throw exception(ERROR);
//        }

        createReqVO.setUserId(getLoginUserId());
        createReqVO.setUserIp(getClientIP());
        createReqVO.setTerminal(getTerminal());
        createReqVO.setSubject("消费分");
        createReqVO.setPayStatus(false);
        createReqVO.setNo(orderNoRedisDAO.generate(OrderNoRedisDAO.TRADE_ORDER_NO_PREFIX));

        // 转换金额
        createReqVO.setPayPrice(createReqVO.getPayPrice() * DistriConstants.ONE_HUNDRED);
        createReqVO.setTopUpConsumptionPoints(createReqVO.getPayPrice() * DistriConstants.PERCENT);

        // 根据充值金额计算实际到账金额
        if(createReqVO.getTopUpConsumptionPoints() >= Double.parseDouble(orderPercentageDO.getTriggerMagnificationPoints()) * DistriConstants.INTEGRAL_PAGE2DB){
        createReqVO.setPracticalConsumptionPoints((long) (createReqVO.getTopUpConsumptionPoints() * Double.parseDouble(orderPercentageDO.getIntegralMagnification())));
        } else {
            createReqVO.setPracticalConsumptionPoints(createReqVO.getTopUpConsumptionPoints().longValue());
        }
        createReqVO.setPercentTemplate(JSON.toJSONString(orderPercentageDO));
    }

    @Override
    public PageResult<UserTopUpConsumptionPointsOrderDO> getUserTopUpConsumptionPointsOrderPage(UserTopUpConsumptionPointsOrderPageReqVO pageReqVO) {
        return userTopUpConsumptionPointsOrderMapper.selectPage(pageReqVO);
    }


}