
package com.tmerclub.cloud.multishop.service.impl;

import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.api.user.feign.UserFeignClient;
import com.tmerclub.cloud.api.user.vo.UserApiVO;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.constant.StatusEnum;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.dto.NoticeDTO;
import com.tmerclub.cloud.multishop.mapper.NoticeMapper;
import com.tmerclub.cloud.multishop.model.Notice;
import com.tmerclub.cloud.multishop.model.ShopDetail;
import com.tmerclub.cloud.multishop.service.NoticeService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.NoticeVO;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

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

/**
 * 公告
 *
 * @author YXF
 * @date 2020-11-25 17:57:56
 */
@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @DubboReference
    private SupplierDetailFeignClient supplierDetailFeignClient;

    @DubboReference
    private UserFeignClient userFeignClient;

    @Autowired
    private ShopDetailService shopDetailService;

    @Override
    public PageVO<NoticeVO> page(PageDTO pageDTO, NoticeDTO noticeDTO) {
        return PageUtil.doPage(pageDTO, () -> noticeMapper.list(noticeDTO));
    }

    @Override
    public NoticeVO getById(Long id) {
        NoticeVO noticeVO = noticeMapper.getById(id);
        if (StringUtils.isNotBlank(noticeVO.getMultiShopIds())) {
            List<Long> multiShopIds = Arrays.stream(noticeVO.getMultiShopIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            List<ShopDetail> shopDetails = shopDetailService.listByShopIds(multiShopIds);
            List<ShopDetailVO> shopDetailList = shopDetails.stream().map(shopDetail -> {
                ShopDetailVO shopDetailVO = new ShopDetailVO();
                shopDetailVO.setShopId(shopDetail.getShopId());
                shopDetailVO.setShopName(shopDetail.getShopName());
                shopDetailVO.setShopStatus(shopDetail.getShopStatus());
                shopDetailVO.setContactName(shopDetail.getContactName());
                shopDetailVO.setContactPhone(shopDetail.getContactPhone());
                return shopDetailVO;
            }).collect(Collectors.toList());
            noticeVO.setShopDetailVOList(shopDetailList);
        }
        if (StringUtils.isNotBlank(noticeVO.getUserIds())) {
            List<Long> userIds = Arrays.stream(noticeVO.getUserIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            //可见范围的用户信息
            ServerResponseEntity<List<UserApiVO>> userResponese = userFeignClient.getUserByUserIds(userIds);
            if (userResponese.isSuccess()) {
                noticeVO.setUserDtailList(userResponese.getData());
            }
        }
        //可见范围的供应商信息
        if (StringUtils.isNotBlank(noticeVO.getSupplierIds())) {
            List<Long> supplierIds = Arrays.stream(noticeVO.getSupplierIds().split(",")).map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
            ServerResponseEntity<List<SupplierApiDetailVO>> supplierResponse = supplierDetailFeignClient.listBySupplierIds(supplierIds);
            if (supplierResponse.isSuccess()) {
                noticeVO.setSupplierDetailList(supplierResponse.getData());
            }
        }

        return noticeVO;
    }

    @Override
    public void updateIsLearning(Long id) {
        noticeMapper.updateIsLearning(id);
    }

    @Override
    public void save(Notice notice) {
        notice.setId(null);
        if (Objects.equals(notice.getStatus(), StatusEnum.ENABLE.value())) {
            notice.setPublishTime(new Date());
        }
        noticeMapper.save(notice);
    }

    @Override
    public void update(Notice notice) {
        noticeMapper.update(notice);
    }

    @Override
    public void deleteById(Long id) {
        noticeMapper.deleteById(id);
    }

    @Override
    @CacheEvict(cacheNames = CacheNames.NOTICES_KEY, key = "#shopId + ':' + #type")
    public void removeCacheByShopIdAndType(Long shopId, Integer type) {

    }

    @Override
    @Cacheable(cacheNames = CacheNames.NOTICES_KEY, key = "#shopId + ':' + #type")
    public List<NoticeVO> getlistByShopId(Long shopId, Integer type) {
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setShopId(shopId);
        noticeDTO.setType(type);
        noticeDTO.setStatus(StatusEnum.ENABLE.value());
        return noticeMapper.listAllByTypeAndShopId(noticeDTO);
    }

    @Override
    public PageVO<NoticeVO> pagePlatformNotice(PageDTO pageDTO, NoticeDTO noticeDTO) {
        return PageUtil.doPage(pageDTO, () -> noticeMapper.list(noticeDTO));
    }

    @Override
    public void saveBatch(List<Notice> noticeList) {
        for (Notice notice : noticeList) {
            notice.setId(null);
            if (Objects.equals(notice.getStatus(), StatusEnum.ENABLE.value())) {
                notice.setPublishTime(new Date());
            }
        }
        noticeMapper.saveBatch(noticeList);
    }
}
