package com.cool.store.service.impl;

import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.RegionAreaConfigDao;
import com.cool.store.dao.RegionDao;
import com.cool.store.entity.RegionAreaConfigDO;
import com.cool.store.entity.RegionDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.ThirdRegionTypeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.RegionAreaConfigAddRequest;
import com.cool.store.service.RegionAreaConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

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

/**
 * @Author wxp
 * @Date 2022/12/29 11:18
 * @Version 1.0
 */
@Service
@Slf4j
public class RegionAreaConfigServiceImpl implements RegionAreaConfigService {

    @Resource
    private RegionAreaConfigDao regionAreaConfigDao;

    @Resource
    private RegionDao regionDao;

    @Override
    public Boolean saveOrUpdateRegionAreaConfig(RegionAreaConfigAddRequest regionAreaConfigAddRequest, LoginUserInfo user) {
        RegionDO regionDO = regionDao.getRegionById(regionAreaConfigAddRequest.getRegionId());
        if(regionDO == null){
            throw new ServiceException(ErrorCodeEnum.REGION_NOT_EXIST);
        }
        if(!ThirdRegionTypeEnum.LARGE_REGION.getCode().equals(regionDO.getThirdRegionType())){
            throw new ServiceException(ErrorCodeEnum.ONLY_WAR_REGION_CONFIG);
        }
        List<Long> areaIdList = regionAreaConfigAddRequest.getAreaIdList();
        regionAreaConfigDao.deleteRegionAreaConfigByRegionId(regionAreaConfigAddRequest.getRegionId());
        if(CollectionUtils.isEmpty(areaIdList)){
            return Boolean.TRUE;
        }
        regionAreaConfigDao.deleteByWantShopAreaIds(areaIdList);
        List<RegionAreaConfigDO> regionAreaConfigList  = new ArrayList<>();
        for (Long areaId: areaIdList) {
            RegionAreaConfigDO regionAreaConfigDO = new RegionAreaConfigDO();
            regionAreaConfigDO.setRegionId(regionAreaConfigAddRequest.getRegionId());
            regionAreaConfigDO.setRegionPath(regionDO.getFullRegionPath());
            regionAreaConfigDO.setWantShopAreaId(areaId);
            regionAreaConfigDO.setCreateUserId(user.getUserId());
            regionAreaConfigDO.setUpdateUserId(user.getUserId());
            regionAreaConfigList.add(regionAreaConfigDO);
        }
        if (CollectionUtils.isNotEmpty(regionAreaConfigList)) {
            regionAreaConfigDao.batchInsertOrUpdateRegionAreaConfig(regionAreaConfigList);
        }
        return Boolean.TRUE;
    }

    @Override
    public List<Long> listAreaIdByRegionId(Long regionId) {
        List<RegionAreaConfigDO> regionAreaConfigDOList = regionAreaConfigDao.listAreaByRegionId(regionId);
        List<Long> areaIdIdList = ListUtils.emptyIfNull(regionAreaConfigDOList)
                .stream()
                .map(RegionAreaConfigDO::getWantShopAreaId)
                .collect(Collectors.toList());
        return areaIdIdList;
    }


}
