package com.slodon.b2b2c.model.business;

import com.slodon.b2b2c.business.dto.OrderSubmitDTO;
import com.slodon.b2b2c.business.dto.OrderSubmitParamDTO;
import com.slodon.b2b2c.business.example.OrderReserveExample;
import com.slodon.b2b2c.business.pojo.OrderReserve;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.business.OrderReserveReadMapper;
import com.slodon.b2b2c.dao.write.business.OrderReserveWriteMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * 虚拟商品订单用户预留信息表model
 */
@Component
@Slf4j
public class OrderReserveModel {

    @Resource
    private OrderReserveReadMapper orderReserveReadMapper;
    @Resource
    private OrderReserveWriteMapper orderReserveWriteMapper;

    /**
     * 新增虚拟商品订单用户预留信息表
     *
     * @param orderReserve
     * @return
     */
    public Integer saveOrderReserve(OrderReserve orderReserve) {
        int count = orderReserveWriteMapper.insert(orderReserve);
        if (count == 0) {
            throw new MallException("添加虚拟商品订单用户预留信息表失败，请重试");
        }
        return count;
    }

    /**
     * 根据reserveId删除虚拟商品订单用户预留信息表
     *
     * @param reserveId reserveId
     * @return
     */
    public Integer deleteOrderReserve(Integer reserveId) {
        if (StringUtils.isEmpty(reserveId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = orderReserveWriteMapper.deleteByPrimaryKey(reserveId);
        if (count == 0) {
            log.error("根据reserveId：" + reserveId + "删除虚拟商品订单用户预留信息表失败");
            throw new MallException("删除虚拟商品订单用户预留信息表失败,请重试");
        }
        return count;
    }

    /**
     * 根据reserveId更新虚拟商品订单用户预留信息表
     *
     * @param orderReserve
     * @return
     */
    public Integer updateOrderReserve(OrderReserve orderReserve) {
        if (StringUtils.isEmpty(orderReserve.getReserveId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = orderReserveWriteMapper.updateByPrimaryKeySelective(orderReserve);
        if (count == 0) {
            log.error("根据reserveId：" + orderReserve.getReserveId() + "更新虚拟商品订单用户预留信息表失败");
            throw new MallException("更新虚拟商品订单用户预留信息表失败,请重试");
        }
        return count;
    }

    /**
     * 根据reserveId获取虚拟商品订单用户预留信息表详情
     *
     * @param reserveId reserveId
     * @return
     */
    public OrderReserve getOrderReserveByReserveId(Integer reserveId) {
        return orderReserveReadMapper.getByPrimaryKey(reserveId);
    }

    /**
     * 根据条件获取虚拟商品订单用户预留信息表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<OrderReserve> getOrderReserveList(OrderReserveExample example, PagerInfo pager) {
        List<OrderReserve> orderReserveList;
        if (pager != null) {
            pager.setRowsCount(orderReserveReadMapper.countByExample(example));
            orderReserveList = orderReserveReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            orderReserveList = orderReserveReadMapper.listByExample(example);
        }
        return orderReserveList;
    }

    /**
     * 提交订单-保存虚拟商品订单用户预留信息
     *
     * @param orderInfo 订单信息
     * @param orderSn   订单号
     */
    public void insertOrderReserve(OrderSubmitDTO.OrderInfo orderInfo, String orderSn) {
        List<OrderSubmitParamDTO.ReserveInfo> reserveInfoList = orderInfo.getReserveInfoList();
        if (!CollectionUtils.isEmpty(reserveInfoList)) {
            //虚拟商品记录预留信息
            reserveInfoList.forEach(reserveInfo -> {
                OrderReserve orderReserve = new OrderReserve();
                orderReserve.setReserveName(reserveInfo.getReserveName());
                orderReserve.setReserveValue(reserveInfo.getReserveValue());
                orderReserve.setOrderSn(orderSn);
                this.saveOrderReserve(orderReserve);
            });
        }
    }
}