package com.cool.store.dao.fees;

import com.cool.store.dto.fees.WalletAllocationDTO;
import com.cool.store.entity.fees.WalletAllocationConfigDO;
import com.cool.store.mapper.fees.WalletAllocationConfigMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 平安钱包分账配置DAO
 *
 * @author system
 */
@Repository
@RequiredArgsConstructor
public class WalletAllocationConfigDAO {
    private final WalletAllocationConfigMapper walletAllocationConfigMapper;

    public void insertSelective(WalletAllocationConfigDO config) {
        walletAllocationConfigMapper.insertSelective(config);
    }

    public void updateByPrimaryKeySelective(WalletAllocationConfigDO config) {
        walletAllocationConfigMapper.updateByPrimaryKeySelective(config);
    }

    public WalletAllocationConfigDO selectByPrimaryKey(Long id) {
        return walletAllocationConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据加盟品牌、招商分部、费用类型查询
     */
    public Boolean existUniqueKey(Integer joinBrand, Long investDivisionId, String expenseType, Long excludeId) {
        Example example = new Example(WalletAllocationConfigDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andEqualTo("investDivisionId", investDivisionId)
                .andEqualTo("expenseType", expenseType);
        if (Objects.nonNull(excludeId)) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return walletAllocationConfigMapper.selectCountByExample(example) > 0;
    }

    /**
     * 根据加盟品牌、招商分部查询
     * @param joinBrand 家门品牌
     * @param investDivisionId 招商分部id
     * @return 配置列表
     */
    public List<WalletAllocationConfigDO> selectByBrandAndInvest(Integer joinBrand, Long investDivisionId) {
        Example example = new Example(WalletAllocationConfigDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andIn("investDivisionId", Arrays.asList(investDivisionId, 0L));
        return walletAllocationConfigMapper.selectByExample(example);
    }



    public List<WalletAllocationDTO> selectByExpenseTypeList(Integer joinBrand, List<String> expenseTypeList) {
        if (joinBrand==null){
            return null;
        }
        return walletAllocationConfigMapper.getAllWalletAllocation(joinBrand,expenseTypeList);
    }

    /**
     * 分页查询
     */
    public List<WalletAllocationConfigDO> selectByCondition(Integer joinBrand, Long investDivisionId, Integer expenseType, List<Long> configIds) {
        Example example = new Example(WalletAllocationConfigDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (joinBrand != null) {
            criteria.andEqualTo("joinBrand", joinBrand);
        }
        if (investDivisionId != null) {
            criteria.andEqualTo("investDivisionId", investDivisionId);
        }
        if (expenseType != null) {
            criteria.andEqualTo("expenseType", expenseType);
        }
        if (CollectionUtils.isNotEmpty(configIds)) {
            criteria.andIn("id", configIds);
        }
        example.setOrderByClause("create_time DESC");
        return walletAllocationConfigMapper.selectByExample(example);
    }

    /**
     * 是否存在其他分部类型
     */
    public boolean existOtherInvestType(Integer joinBrand, String expenseType, Long investDivisionId, Long excludeId) {
        Example example = new Example(WalletAllocationConfigDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andEqualTo("expenseType", expenseType);
        if (Long.valueOf(0).equals(investDivisionId)) {
            criteria.andNotEqualTo("investDivisionId", 0);
        } else {
            criteria.andEqualTo("investDivisionId", 0);
        }
        if (Objects.nonNull(excludeId)) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return walletAllocationConfigMapper.selectCountByExample(example) > 0;
    }
}

