package com.cool.store.dao.fees;

import com.cool.store.entity.fees.PayeeBankConfigDO;
import com.cool.store.mapper.fees.PayeeBankConfigMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

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

/**
 * 线下收款银行配置DAO
 */
@Repository
@RequiredArgsConstructor
public class PayeeBankConfigDAO {
    private final PayeeBankConfigMapper payeeBankConfigMapper;

    public void insertSelective(PayeeBankConfigDO config) {
        payeeBankConfigMapper.insertSelective(config);
    }

    public void updateByPrimaryKeySelective(PayeeBankConfigDO config) {
        payeeBankConfigMapper.updateByPrimaryKeySelective(config);
    }

    public PayeeBankConfigDO selectByPrimaryKey(Long id) {
        return payeeBankConfigMapper.selectByPrimaryKey(id);
    }

    public boolean existUniqueKey(Integer joinBrand, Long investDivisionId, Integer payType, Long excludeId) {
        Example example = new Example(PayeeBankConfigDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andEqualTo("investDivisionId", investDivisionId)
                .andEqualTo("payType", payType);
        if (Objects.nonNull(excludeId)) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return payeeBankConfigMapper.selectCountByExample(example) > 0;
    }

    /**
     * 同品牌同支付方式：只能存在“全部(0)”或“分部”其中一种
     */
    public boolean existOtherInvestType(Integer joinBrand, Integer payType, Long investDivisionId, Long excludeId) {
        Example example = new Example(PayeeBankConfigDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andEqualTo("payType", payType);
        if (Long.valueOf(0).equals(investDivisionId)) {
            criteria.andNotEqualTo("investDivisionId", 0L);
        } else {
            criteria.andEqualTo("investDivisionId", 0L);
        }
        if (Objects.nonNull(excludeId)) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return payeeBankConfigMapper.selectCountByExample(example) > 0;
    }

    public List<PayeeBankConfigDO> selectByCondition(Integer joinBrand, Long investDivisionId, Integer payType) {
        Example example = new Example(PayeeBankConfigDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (joinBrand != null) {
            criteria.andEqualTo("joinBrand", joinBrand);
        }
        if (investDivisionId != null) {
            criteria.andEqualTo("investDivisionId", investDivisionId);
        }
        if (payType != null) {
            criteria.andEqualTo("payType", payType);
        }
        example.setOrderByClause("create_time DESC");
        return payeeBankConfigMapper.selectByExample(example);
    }

    /**
     * 根据加盟品牌、加盟分部、支付方式查询配置
     * @param joinBrand 加盟品牌
     * @param investDivisionId 加盟分部
     * @param payType 支付方式
     * @return 配置
     */
    public PayeeBankConfigDO getByUniqueKey(Integer joinBrand, Long investDivisionId, Integer payType) {
        Example example = new Example(PayeeBankConfigDO.class);
        example.createCriteria()
                .andEqualTo("joinBrand", joinBrand)
                .andIn("investDivisionId", Arrays.asList(investDivisionId, 0L))
                .andEqualTo("payType", payType);
        return payeeBankConfigMapper.selectOneByExample(example);
    }
}


