package com.buy507.mall.portal.service.impl;

import cn.hutool.db.sql.Order;
import com.buy507.mall.common.api.CommonResult;
import com.buy507.mall.mapper.OmsOrderMapper;
import com.buy507.mall.model.MemberLevel;
import com.buy507.mall.model.OmsOrder;
import com.buy507.mall.portal.domain.OrderParam;
import com.buy507.mall.portal.domain.OrderResult;
import com.buy507.mall.portal.service.RedisService;
import com.buy507.mall.portal.service.VipCenterService;
import com.buy507.mall.portal.vo.OmsOrderDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class VipCenterServiceImpl implements VipCenterService {

    //店长升级金额
    @Value("${storeManagerAmount}")
    private BigDecimal storeManagerAmount;

    //代理升级金额
    @Value("${agentAmount}")
    private BigDecimal agentAmount;

    //市代理升级金额
    @Value("${cityAgentAmount}")
    private BigDecimal cityAgentAmount;

    @Value("${redis.key.prefix.orderId}")
    private String REDIS_KEY_PREFIX_ORDER_ID;

    @Autowired
    private RedisService redisService;

    @Autowired
    private OmsOrderMapper orderMapper;

    @Override
    public BigDecimal getUpgradeAmount(Long memberLevelId) {
        BigDecimal amount = null;
        if (MemberLevel.Partner.getValue() == memberLevelId){
            amount = storeManagerAmount;
        }
        if (MemberLevel.Diamond.getValue() == memberLevelId){
            amount = agentAmount;
        }
        if (MemberLevel.OneDiamond.getValue() == memberLevelId){
            amount = cityAgentAmount;
        }

        return amount;
    }

    /**
     * 创建购买权益订单
     * @param param
     * @return
     */
    @Override
    public CommonResult createUpgradeOrder(OmsOrderDTO param) {
        if (param == null){
            return CommonResult.validateFailed("订单数据为空");
        }else {
            //创建权益订单
            OmsOrder order = new OmsOrder();
            BeanUtils.copyProperties(param, order);
            order.setOrderSn(generateUpgradeOrderSn(param));  //生成orderSn
            order.setCreateTime(new Date());  //创建订单时间
            order.setStatus(0); //订单状态--未支付
            order.setOrderType(3); //订单类型--虚拟订单
            orderMapper.insert(order);

            //返回订单id
            Map<String, Object> result = new HashMap<>();
            result.put("orderId", order.getId());
            return CommonResult.success(result, "下单成功");
        }
    }

    /**
     * 生成18位订单编号:8位日期+2位订单类型+2位支付方式+6位以上自增id
     */
    private String generateUpgradeOrderSn(OmsOrderDTO param) {
        StringBuilder sb = new StringBuilder();
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String key = REDIS_KEY_PREFIX_ORDER_ID + date;
        Long increment = redisService.increment(key, 1);
        sb.append(date);
        sb.append(String.format("%02d", param.getOrderType()));
        sb.append(String.format("%02d", param.getPayType()));
        String incrementStr = increment.toString();
        if (incrementStr.length() <= 6) {
            sb.append(String.format("%06d", increment));
        } else {
            sb.append(incrementStr);
        }
        return sb.toString();
    }
}
