package com.cool.store.dao.wallet;

import com.cool.store.entity.wallet.OpenBankInfoDO;
import com.cool.store.mapper.wallet.OpenBankInfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

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

/**
 * @Author suzhuhong
 * @Date 2025/11/20 10:37
 * @Version 1.0
 */
@Repository
public class OpenBankInfoDAO {


    @Resource
    private OpenBankInfoMapper openBankInfoMapper;

    public int insertSelective(OpenBankInfoDO openBankInfoDO){
        return openBankInfoMapper.insertSelective(openBankInfoDO);
    }

    public int updateByStoreCode(OpenBankInfoDO openBankInfoDO){
        Example example = new Example(OpenBankInfoDO.class);
        example.createCriteria()
                .andEqualTo("storeCode", openBankInfoDO.getStoreCode());
        return openBankInfoMapper.updateByExampleSelective(openBankInfoDO, example);
    }

    public OpenBankInfoDO getOpenBankInfo(String storeCode){
        return openBankInfoMapper.getOpenBankInfo(storeCode);
    }

    public OpenBankInfoDO getOpenBankInfoByStoreId(String storeId) {
        return openBankInfoMapper.selectOne(OpenBankInfoDO.builder().storeId(storeId).build());
    }

    public int insertOrUpdateByStoreId(OpenBankInfoDO openBankInfoDO) {
        if (StringUtils.isBlank(openBankInfoDO.getStoreId())) {
            return 0;
        }
        if (Objects.isNull(getOpenBankInfo(openBankInfoDO.getStoreId()))) {
            return openBankInfoMapper.insertSelective(openBankInfoDO);
        } else {
            Example example = new Example(OpenBankInfoDO.class);
            example.createCriteria().andEqualTo("storeId", openBankInfoDO.getStoreId());
            return openBankInfoMapper.updateByExampleSelective(openBankInfoDO, example);
        }
    }
}
