package com.slodon.b2b2c.model.seller;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.seller.StoreWithdrawalReadMapper;
import com.slodon.b2b2c.dao.write.seller.StoreWithdrawalWriteMapper;
import com.slodon.b2b2c.seller.example.StoreWithdrawalExample;
import com.slodon.b2b2c.seller.pojo.StoreWithdrawal;
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 StoreWithdrawalModel {
    @Resource
    private StoreWithdrawalReadMapper storeWithdrawalReadMapper;

    @Resource
    private StoreWithdrawalWriteMapper storeWithdrawalWriteMapper;

    /**
     * 新增商户提现记录表
     *
     * @param storeWithdrawal 商户提现记录信息
     * @return return
     */
    public Integer saveStoreWithdrawal(StoreWithdrawal storeWithdrawal) {
        int count = storeWithdrawalWriteMapper.insert(storeWithdrawal);
        if (count == 0) {
            throw new MallException("添加商户提现记录表失败，请重试");
        }
        return count;
    }

    /**
     * 根据withdrawalId删除商户提现记录表
     *
     * @param withdrawalId withdrawalId
     * @return return
     */
    public Integer deleteStoreWithdrawal(Integer withdrawalId) {
        if (StringUtils.isEmpty(withdrawalId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = storeWithdrawalWriteMapper.deleteByPrimaryKey(withdrawalId);
        if (count == 0) {
            log.error("根据withdrawalId：" + withdrawalId + "删除商户提现记录表失败");
            throw new MallException("删除商户提现记录表失败,请重试");
        }
        return count;
    }

    /**
     * 根据withdrawalId更新商户提现记录表
     *
     * @param storeWithdrawal 商户提现记录信息
     * @return return
     */
    public Integer updateStoreWithdrawal(StoreWithdrawal storeWithdrawal) {
        if (StringUtils.isEmpty(storeWithdrawal.getWithdrawalId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = storeWithdrawalWriteMapper.updateByPrimaryKeySelective(storeWithdrawal);
        if (count == 0) {
            log.error("根据withdrawalId：" + storeWithdrawal.getWithdrawalId() + "更新商户提现记录表失败");
            throw new MallException("更新商户提现记录表失败,请重试");
        }
        return count;
    }

    /**
     * 根据withdrawalId获取商户提现记录表详情
     *
     * @param withdrawalId withdrawalId
     * @return return
     */
    public StoreWithdrawal getStoreWithdrawalByWithdrawalId(Integer withdrawalId) {
        return storeWithdrawalReadMapper.getByPrimaryKey(withdrawalId);
    }

    /**
     * 根据withdrawalSn获取商户提现记录表详情
     *
     * @param withdrawalSn withdrawalSn
     * @return return
     */
    public StoreWithdrawal getStoreWithdrawalByWithdrawalSn(String withdrawalSn) {
        StoreWithdrawalExample storeWithdrawalExample = new StoreWithdrawalExample();
        storeWithdrawalExample.setWithdrawalSn(withdrawalSn);
        List<StoreWithdrawal> storeWithdrawalList = storeWithdrawalReadMapper.listByExample(storeWithdrawalExample);
        if (CollectionUtils.isEmpty(storeWithdrawalList)){
            return null;
        }
        return storeWithdrawalList.get(0);
    }

    /**
     * 根据条件获取商户提现记录表列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return return
     */
    public List<StoreWithdrawal> getStoreWithdrawalList(StoreWithdrawalExample example, PagerInfo pager) {
        List<StoreWithdrawal> storeWithdrawalList;
        if (pager != null) {
            pager.setRowsCount(storeWithdrawalReadMapper.countByExample(example));
            storeWithdrawalList = storeWithdrawalReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            storeWithdrawalList = storeWithdrawalReadMapper.listByExample(example);
        }
        return storeWithdrawalList;
    }
}