package com.cool.store.dao;

import com.cool.store.dto.region.BigRegionDTO;
import com.cool.store.entity.BigRegionDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.FranchiseBrandEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.BigRegionMapper;
import com.cool.store.request.QueryBigRegionRequest;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2025/5/9 14:25
 * @Version 1.0
 */
@Repository
public class BigRegionDAO {

    @Resource
    BigRegionMapper bigRegionMapper;

    public List<BigRegionDTO> queryAllBigRegion(String keyword, Integer joinBrand,Integer type){
        return bigRegionMapper.queryAllBigRegion(keyword, joinBrand,type);
    }

    public BigRegionDO queryOrgInfoByBigRegionAndJoinMode(Long regionId, Integer joinMode){
        if (regionId==null||joinMode==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        return bigRegionMapper.queryOrgInfoByBigRegionAndJoinMode(regionId,joinMode);
    }

    public List<Long> getRegionIdByGroupName(List<String> groupNameList){
        if (CollectionUtils.isEmpty(groupNameList)){
            return  null;
        }
        Example example = new Example(BigRegionDO.class);
        example.createCriteria().andIn("groupName", groupNameList);
        List<BigRegionDO> bigRegionDOS = bigRegionMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(bigRegionDOS)){
            return null;
        }
        return bigRegionDOS.stream().map(BigRegionDO::getRegionId).collect(Collectors.toList());
    }
    public List<BigRegionDO> getByRegionIdList(List<Long> regionIdList){
        if (CollectionUtils.isEmpty(regionIdList)){
            return  null;
        }
        Example example = new Example(BigRegionDO.class);
        example.createCriteria().andIn("regionId", regionIdList).andEqualTo("joinMode",0);
        return bigRegionMapper.selectByExample(example);
    }
    /**
     * @Auther: wangshuo
     * @Date: 2025/5/15
     * @description:只查询joinMode = 0
     */
    public List<BigRegionDTO> queryBigRegion(QueryBigRegionRequest queryBigRegionRequest){
        return bigRegionMapper.queryBigRegion(queryBigRegionRequest);
    }

    public BigRegionDO getByRegionId(Long regionId){
        if (regionId==null){
            throw  new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        Example example = new Example(BigRegionDO.class);
        example.createCriteria().andEqualTo("regionId", regionId);
        example.setOrderByClause("id ASC limit 1");
        return bigRegionMapper.selectOneByExample(example);
    }
    public Integer updateHqtDate(BigRegionDO bigRegionDO){
        return bigRegionMapper.updateHqtDate(bigRegionDO);
    }

}
