package com.gic.haoban.manage.service.service.hm.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.enterprise.api.dto.StoreDTO;
import com.gic.enterprise.api.service.StoreService;
import com.gic.haoban.manage.api.dto.hm.HmLinkStoreDTO;
import com.gic.haoban.manage.service.dao.mapper.hm.HmLinkStoreMapper;
import com.gic.haoban.manage.service.entity.hm.TabHmLinkStore;
import com.gic.haoban.manage.service.service.hm.HmLinkStoreService;

@Service("hmLinkStoreService")
public class HmLinkStoreServiceImpl implements HmLinkStoreService {
    
    private static org.apache.logging.log4j.Logger log = LogManager.getLogger(HmLinkStoreService.class);
    
    @Autowired
    private HmLinkStoreMapper hmLinkStoreMapper;
    @Autowired
    private StoreService storeService ;

    @Override
    public List<HmLinkStoreDTO> listByLinkId(String wxEnterpriseId, String enterpriseId, Long linkId) {
        List<TabHmLinkStore> list = this.hmLinkStoreMapper.listByLinkId(linkId);
        if (CollectionUtils.isNotEmpty(list)) {
            List<HmLinkStoreDTO> storeList = EntityUtil.changeEntityListByJSON(HmLinkStoreDTO.class, list);
            List<String> storeIdList = list.stream().map(dto->dto.getStoreId()).collect(Collectors.toList()) ;
            List<StoreDTO> storeDTOList = this.storeService.queryStore(storeIdList);
            Map<String,String> map = storeDTOList.stream().collect(Collectors.toMap(StoreDTO::getStoreId, StoreDTO::getStoreName,(k1,k2)->k1)) ;
            for(HmLinkStoreDTO item : storeList) {
                if(null != map.get(item.getStoreId())) {
                    item.setStoreName(map.get((item.getStoreId())));
                }
            }
            return storeList;
        }
        return null;
    }

    @Override
    @Transactional
    public int saveStore(Long linkId, List<HmLinkStoreDTO> thisList) {
        if (CollectionUtils.isEmpty(thisList)) {
            this.hmLinkStoreMapper.deleteByLinkId(linkId);
            return 0;
        }
        int count = 0;
        // 本次新加的
        List<TabHmLinkStore> addList = new ArrayList<>();
        List<Long> delIdList = new ArrayList<>();
        List<String> oldStoreIdList = new ArrayList<>();
        List<TabHmLinkStore> oldList = this.hmLinkStoreMapper.listByLinkId(linkId);
        if (CollectionUtils.isNotEmpty(oldList)) {
            oldStoreIdList = oldList.stream().map(dto -> dto.getStoreId()).collect(Collectors.toList());
        }
        for (HmLinkStoreDTO dto : thisList) {
            if (!oldStoreIdList.contains(dto.getStoreId())) {
                addList.add(EntityUtil.changeEntityByJSON(TabHmLinkStore.class, dto));
            }
        }
        List<String> thisStoreIdList = thisList.stream().map(dto -> dto.getStoreId()).collect(Collectors.toList());
        for (TabHmLinkStore item : oldList) {
            if (!thisStoreIdList.contains(item.getStoreId())) {
                delIdList.add(item.getId());
            }
        }
        if (CollectionUtils.isNotEmpty(addList)) {
            for (TabHmLinkStore item : addList) {
                item.setId(UniqueIdUtils.uniqueLong());
                item.setLinkId(linkId);
                item.setSetttingType(1);
                item.setStatusFlag(1);
            }
            this.hmLinkStoreMapper.insertBatch(addList);
        }
        if (CollectionUtils.isNotEmpty(delIdList)) {
            this.hmLinkStoreMapper.deleteById(delIdList);
        }
        return count;
    }

}
