package com.cool.store.dao.store;

import com.cool.store.entity.store.StoreTypeDO;
import com.cool.store.mapper.store.StoreTypeMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 店型 DAO
 */
@Repository
@RequiredArgsConstructor
public class StoreTypeDAO {
    private final StoreTypeMapper storeTypeMapper;

    /** 新增 */
    public boolean insertSelective(StoreTypeDO storeTypeDO) {
        return storeTypeMapper.insertSelective(storeTypeDO) > 0;
    }

    /** 编辑 */
    public boolean updateSelective(StoreTypeDO storeTypeDO) {
        return storeTypeMapper.updateByPrimaryKeySelective(storeTypeDO) > 0;
    }

    /** 根据主键查询 */
    public StoreTypeDO getById(Long id) {
        return storeTypeMapper.selectByPrimaryKey(id);
    }

    /** 根据编码查询 */
    public StoreTypeDO getByCode(String code) {
        return storeTypeMapper.selectOne(StoreTypeDO.builder().storeTypeCode(code).build());
    }

    /** 批量删除(物理删除，谨慎使用) */
    public boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        Example example = new Example(StoreTypeDO.class);
        example.createCriteria().andIn("id", ids);
        return storeTypeMapper.deleteByExample(example) > 0;
    }

    public List<StoreTypeDO> selectByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        Example example = new Example(StoreTypeDO.class);
        example.createCriteria().andIn("id", ids);
        return storeTypeMapper.selectByExample(example);
    }

    /**
     * 软删除：deleted=true
     */
    public boolean softDeleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        StoreTypeDO update = new StoreTypeDO();
        update.setDeleted(true);
        Example example = new Example(StoreTypeDO.class);
        example.createCriteria().andIn("id", ids);
        return storeTypeMapper.updateByExampleSelective(update, example) > 0;
    }

    /**
     * 启用/停用
     */
    public boolean updateActiveById(Long id, Boolean active) {
        if (id == null) {
            return false;
        }
        StoreTypeDO update = new StoreTypeDO();
        update.setId(id);
        update.setActive(active);
        return storeTypeMapper.updateByPrimaryKeySelective(update) > 0;
    }

    /**
     * 分页/条件查询
     * @param keyword 名称/编码模糊
     * @param active 是否启用
     * @param brand 品牌精准
     */
    public List<StoreTypeDO> queryByCondition(String keyword, Boolean active, String brand) {
        return storeTypeMapper.queryByCondition(keyword, active, brand);
    }

    /**
     * 根据门店类型查询启用的店型
     */
    public List<StoreTypeDO> listActiveByStoreType(Integer storeType) {
        return storeTypeMapper.listByStoreType(storeType);
    }

    /**
     * 校验排序是否已存在（全局唯一）
     */
    public boolean existsSortOrder(Integer sortOrder, Long excludeId) {
        if (sortOrder == null) {
            return false;
        }
        Example example = new Example(StoreTypeDO.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("sortOrder", sortOrder);
        criteria.andEqualTo("deleted", 1);
        if (excludeId != null) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return storeTypeMapper.selectCountByExample(example) > 0;
    }
}
