package com.slodon.b2b2c.model.integral;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.integral.IntegralOrderReserveReadMapper;
import com.slodon.b2b2c.dao.write.integral.IntegralOrderReserveWriteMapper;
import com.slodon.b2b2c.integral.dto.OrderSubmitParamDTO;
import com.slodon.b2b2c.integral.example.IntegralOrderReserveExample;
import com.slodon.b2b2c.integral.pojo.IntegralOrderReserve;
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 IntegralOrderReserveModel {

    @Resource
    private IntegralOrderReserveReadMapper integralOrderReserveReadMapper;
    @Resource
    private IntegralOrderReserveWriteMapper integralOrderReserveWriteMapper;

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

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

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

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

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

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