package com.cool.store.dao.fees;

import cn.hutool.core.collection.CollStreamUtil;
import com.cool.store.entity.fees.WalletAllocationCompanyDO;
import com.cool.store.mapper.fees.WalletAllocationCompanyMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 平安钱包分账公司配置DAO
 *
 * @author system
 */
@Repository
@RequiredArgsConstructor
public class WalletAllocationCompanyDAO {
    private final WalletAllocationCompanyMapper walletAllocationCompanyMapper;

    public void insertSelective(WalletAllocationCompanyDO company) {
        walletAllocationCompanyMapper.insertSelective(company);
    }

    /**
     * 批量新增
     */
    public void insertBatch(List<WalletAllocationCompanyDO> list) {
        if (CollectionUtils.isNotEmpty(list)) {
            walletAllocationCompanyMapper.insertBatch(list);
        }
    }

    public void updateByPrimaryKeySelective(WalletAllocationCompanyDO company) {
        walletAllocationCompanyMapper.updateByPrimaryKeySelective(company);
    }

    public WalletAllocationCompanyDO selectByPrimaryKey(Long id) {
        return walletAllocationCompanyMapper.selectByPrimaryKey(id);
    }

    public void deleteByPrimaryKey(Long id) {
        walletAllocationCompanyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据分账配置id查询公司列表
     */
    public List<WalletAllocationCompanyDO> selectByConfigId(Long configId) {
        Example example = new Example(WalletAllocationCompanyDO.class);
        example.createCriteria().andEqualTo("configId", configId);
        return walletAllocationCompanyMapper.selectByExample(example);
    }

    /**
     * 根据配置id列表查询收款公司映射
     */
    public Map<Long, List<WalletAllocationCompanyDO>> selectGroupByConfigIds(List<Long> configIds) {
        if (CollectionUtils.isEmpty(configIds)) {
            return Collections.emptyMap();
        }
        Example example = new Example(WalletAllocationCompanyDO.class);
        example.createCriteria().andIn("configId", configIds);
        List<WalletAllocationCompanyDO> list = walletAllocationCompanyMapper.selectByExample(example);
        return CollStreamUtil.groupByKey(list, WalletAllocationCompanyDO::getConfigId);
    }

    /**
     * 根据收款公司code查询（用于分页查询条件）
     */
    public List<WalletAllocationCompanyDO> selectByPayeeCode(String payeeCode) {
        Example example = new Example(WalletAllocationCompanyDO.class);
        example.createCriteria().andEqualTo("payeeCode", payeeCode);
        return walletAllocationCompanyMapper.selectByExample(example);
    }

    /**
     * 根据配置id删除
     */
    public void deleteByConfigId(Long configId) {
        WalletAllocationCompanyDO companyDO = new WalletAllocationCompanyDO();
        companyDO.setConfigId(configId);
        walletAllocationCompanyMapper.delete(companyDO);
    }
}

