package jnpf.store.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.StoreRegion;
import jnpf.model.store.StoreRegionVo;
import jnpf.store.mapper.StoreRegionMapper;
import jnpf.store.service.StoreRegionService;
import jnpf.util.RandomUtil;
import jnpf.util.UserProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 门店区域管理表
 */
@Slf4j
@Service("ftbStoreRegionServiceImpl")
public class StoreRegionServiceImpl extends SuperServiceImpl<StoreRegionMapper, StoreRegion> implements StoreRegionService {
    @Autowired
    private UserProvider userProvider;

    @Override
    public void batchSaveOrUpdateOrDelete(String storeId, List<StoreRegionVo> regionList) {
        List<StoreRegion> allList = lambdaQuery().eq(StoreRegion::getStoreId, storeId).orderByDesc(StoreRegion::getCreatorTime).list();
        List<String> regions = allList.stream().filter(m -> m.getDeleteMark().equals(0)).map(StoreRegion::getId).collect(Collectors.toList());
        //新增数据
        List<StoreRegionVo> regionAddList = regionList.stream().filter(item -> !regions.contains(item.getId())).collect(Collectors.toList());
        //修改数据
        List<StoreRegionVo> regionUpdateList = regionList.stream().filter(item -> regions.contains(item.getId())).collect(Collectors.toList());
        //删除的数据
        List<String> regionReqIdList = regionList.stream().map(StoreRegionVo::getId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<String> regionDeleteIdList = new ArrayList<>(CollUtil.subtract(regions, regionReqIdList));
        List<StoreRegion> regionDeleteList = allList.stream().filter(item -> regionDeleteIdList.contains(item.getId())).collect(Collectors.toList());
        List<StoreRegion> storeRegionVos = CollUtil.newArrayList();
        List<StoreRegion> storeRegionVoUpdateList = CollUtil.newArrayList();
        Date newDate = new Date();
        String userId = userProvider.get().getUserId();
        for (StoreRegionVo regionVo : regionAddList) {
            StoreRegion storeRegion = new StoreRegion();
            BeanUtils.copyProperties(regionVo, storeRegion);
            this.initStoreRegionEntity(storeId, storeRegion);
            storeRegionVos.add(storeRegion);
        }
        for (StoreRegionVo regionVo : regionUpdateList) {
            StoreRegion region = allList.stream().filter(t -> t.getId().equals(regionVo.getId())).findFirst().orElse(null);
            BeanUtils.copyProperties(regionVo, region);
            storeRegionVoUpdateList.add(region);
        }
        for (StoreRegion regionVo : regionDeleteList) {
            regionVo.setDeleteMark(1);
            regionVo.setDeleteTime(newDate);
            regionVo.setDeleteUserId(userId);
            storeRegionVoUpdateList.add(regionVo);
        }
        if (CollUtil.isNotEmpty(storeRegionVos)) {
            saveBatch(storeRegionVos);
        }
        if (CollUtil.isNotEmpty(storeRegionVoUpdateList)) {
            updateBatchById(storeRegionVoUpdateList);
        }
    }

    @Override
    public List<StoreRegion> getStoreRegionList(String storeId, List<StoreRegionVo> regionList) {
        List<StoreRegion> allList = lambdaQuery().eq(StoreRegion::getStoreId, storeId).orderByDesc(StoreRegion::getCreatorTime).list();
        List<String> regions = allList.stream().filter(m -> m.getDeleteMark().equals(0)).map(StoreRegion::getId).collect(Collectors.toList());
        //新增数据
        List<StoreRegionVo> regionAddList = regionList.stream().filter(item -> !regions.contains(item.getId())).collect(Collectors.toList());
        //修改数据
        List<StoreRegionVo> regionUpdateList = regionList.stream().filter(item -> regions.contains(item.getId())).collect(Collectors.toList());
        //删除的数据
        List<String> regionReqIdList = regionList.stream().map(StoreRegionVo::getId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<String> regionDeleteIdList = new ArrayList<>(CollUtil.subtract(regions, regionReqIdList));
        List<StoreRegion> regionDeleteList = allList.stream().filter(item -> regionDeleteIdList.contains(item.getId())).collect(Collectors.toList());
        List<StoreRegion> storeRegionVos = CollUtil.newArrayList();
        Date newDate = new Date();
        String userId = userProvider.get().getUserId();
        for (StoreRegionVo regionVo : regionAddList) {
            StoreRegion storeRegion = new StoreRegion();
            BeanUtils.copyProperties(regionVo, storeRegion);
            this.initStoreRegionEntity(storeId, storeRegion);
            storeRegionVos.add(storeRegion);
        }
        for (StoreRegionVo regionVo : regionUpdateList) {
            StoreRegion region = allList.stream().filter(t -> t.getId().equals(regionVo.getId())).findFirst().orElse(null);
            BeanUtils.copyProperties(regionVo, region);
            storeRegionVos.add(region);
        }
        for (StoreRegion regionVo : regionDeleteList) {
            regionVo.setDeleteMark(1);
            regionVo.setDeleteTime(newDate);
            regionVo.setDeleteUserId(userId);
            storeRegionVos.add(regionVo);
        }
        return storeRegionVos;
    }

    private void initStoreRegionEntity(String storeId, StoreRegion storeRegion) {
        String userId = userProvider.get().getUserId();
        DateTime nowTime = DateTime.now();
        storeRegion.setId(RandomUtil.uuId());
        storeRegion.setStoreId(storeId);
        storeRegion.setCreatorUserId(userId);
        storeRegion.setCreatorTime(nowTime);
        storeRegion.setLastModifyUserId(userId);
        storeRegion.setLastModifyTime(nowTime);
    }
}
