package com.slodon.b2b2c.model.system;

import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.dao.read.system.ZjBankReadMapper;
import com.slodon.b2b2c.dao.write.system.ZjBankWriteMapper;
import com.slodon.b2b2c.system.example.ZjBankExample;
import com.slodon.b2b2c.system.pojo.ZjBank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

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

/**
 * 银行表，用于中金网银支付model
 */
@Component
@Slf4j
public class ZjBankModel {
    @Resource
    private ZjBankReadMapper zjBankReadMapper;

    @Resource
    private ZjBankWriteMapper zjBankWriteMapper;

    /**
     * 新增银行表，用于中金网银支付
     *
     * @param zjBank 银行信息，用于中金网银支付
     * @return
     */
    public Integer saveZjBank(ZjBank zjBank) {
        int count = zjBankWriteMapper.insert(zjBank);
        if (count == 0) {
            throw new MallException("添加银行表，用于中金网银支付失败，请重试");
        }
        return count;
    }

    /**
     * 根据bankId删除银行表，用于中金网银支付
     *
     * @param bankId bankId
     * @return
     */
    public Integer deleteZjBank(Integer bankId) {
        if (StringUtils.isEmpty(bankId)) {
            throw new MallException("请选择要删除的数据");
        }
        int count = zjBankWriteMapper.deleteByPrimaryKey(bankId);
        if (count == 0) {
            log.error("根据bankId：" + bankId + "删除银行表，用于中金网银支付失败");
            throw new MallException("删除银行表，用于中金网银支付失败,请重试");
        }
        return count;
    }

    /**
     * 根据bankId更新银行表，用于中金网银支付
     *
     * @param zjBank 银行信息，用于中金网银支付
     * @return
     */
    public Integer updateZjBank(ZjBank zjBank) {
        if (StringUtils.isEmpty(zjBank.getBankId())) {
            throw new MallException("请选择要修改的数据");
        }
        int count = zjBankWriteMapper.updateByPrimaryKeySelective(zjBank);
        if (count == 0) {
            log.error("根据bankId：" + zjBank.getBankId() + "更新银行表，用于中金网银支付失败");
            throw new MallException("更新银行表，用于中金网银支付失败,请重试");
        }
        return count;
    }

    /**
     * 根据bankId获取银行表，用于中金网银支付详情
     *
     * @param bankId bankId
     * @return
     */
    public ZjBank getZjBankByBankId(Integer bankId) {
        return zjBankReadMapper.getByPrimaryKey(bankId);
    }

    /**
     * 根据条件获取银行表，用于中金网银支付列表
     *
     * @param example 查询条件信息
     * @param pager   分页信息
     * @return
     */
    public List<ZjBank> getZjBankList(ZjBankExample example, PagerInfo pager) {
        List<ZjBank> zjBankList;
        if (pager != null) {
            pager.setRowsCount(zjBankReadMapper.countByExample(example));
            zjBankList = zjBankReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            zjBankList = zjBankReadMapper.listByExample(example);
        }
        return zjBankList;
    }
}