package com.unis.module.coding.service.bank;

import com.unis.module.coding.controller.admin.bank.vo.BankPageReqVO;
import com.unis.module.coding.controller.admin.bank.vo.BankSaveReqVO;
import com.unis.module.coding.dal.dataobject.bank.BankDO;
import com.unis.module.coding.dal.mysql.bank.BankMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.unis.framework.common.pojo.PageResult;
import com.unis.framework.common.pojo.PageParam;
import com.unis.framework.common.util.object.BeanUtils;
import static com.unis.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.unis.module.system.enums.ErrorCodeConstants.BANK_NOT_EXISTS;


/**
 * 银行已选 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class BankServiceImpl implements BankService {

    @Resource
    private BankMapper bankMapper;

    @Override
    public Long createBank(BankSaveReqVO createReqVO) {
        // 插入
        BankDO bank = BeanUtils.toBean(createReqVO, BankDO.class);
        bankMapper.insert(bank);
        // 返回
        return bank.getId();
    }

    @Override
    public void updateBank(BankSaveReqVO updateReqVO) {
        // 校验存在
        validateBankExists(updateReqVO.getId());
        // 更新
        BankDO updateObj = BeanUtils.toBean(updateReqVO, BankDO.class);
        bankMapper.updateById(updateObj);
    }

    @Override
    public void deleteBank(Long id) {
        // 校验存在
        validateBankExists(id);
        // 删除
        bankMapper.deleteById(id);
    }

    private void validateBankExists(Long id) {
        if (bankMapper.selectById(id) == null) {
            throw exception(BANK_NOT_EXISTS);
        }
    }

    @Override
    public BankDO getBank(Long id) {
        return bankMapper.selectById(id);
    }

    @Override
    public PageResult<BankDO> getBankPage(BankPageReqVO pageReqVO) {
        return bankMapper.selectPage(pageReqVO);
    }

}