package com.xxfc.platform.tour.biz;

import com.github.wxiaoqi.security.admin.feign.dto.UserDTO;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.tour.dto.TourTagBannerDTO;
import com.xxfc.platform.tour.mapper.TourTagBannerMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.xxfc.platform.tour.entity.TourTagBanner;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 标签轮播图
 *
 * @author zjw
 * @email nishijjo@qq.com
 * @date 2019-06-06 11:41:51
 */
@Service
@Slf4j
public class TourTagBannerBiz extends BaseBiz<TourTagBannerMapper,TourTagBanner> {

    public List<TourTagBanner> findById(long tid) {
        TourTagBanner tourTagBanner = new TourTagBanner();
        tourTagBanner.setId(tid);
        tourTagBanner.setIsDel(0);
        return  mapper.select(tourTagBanner);
    }


    /**
     * 分页查询旅游标签banner数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageDataVO<TourTagBanner> findTourTagBannerWithPage(Integer pageNo,Integer pageSize){
        return  PageDataVO.pageInfo(pageNo,pageSize,()->mapper.findAllByState(0));
    }

    /**
     * 根据id更改banner状态
     * @param id
     * @param status
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateTourTagBannerStatusById(int id, int status) {

        return mapper.updateTourTagBannerStatusById(id,status);
    }

    /**
     * 保存标签轮播图
     * @param tourTagBannerDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int save(TourTagBannerDTO tourTagBannerDTO, UserDTO userDTO) {
        if (log.isDebugEnabled()){
            log.debug("保存的轮播图信息：[{}]",tourTagBannerDTO);
        }
        TourTagBanner tourTagBanner = new TourTagBanner();
        BeanUtils.copyProperties(tourTagBannerDTO,tourTagBanner);
        //设置相关信息
        tourTagBanner.setCrtTime(Instant.now().toEpochMilli());
        tourTagBanner.setCrtName(userDTO.getUsername());
        tourTagBanner.setIsDel(0);
        return mapper.insert(tourTagBanner);
    }

    /**
     * 批量保存
     * @param tourTagBannerDTOS
     * @param userDTO
     * @return
     */
    public int saveBatch(List<TourTagBannerDTO> tourTagBannerDTOS,UserDTO userDTO){
        if (log.isDebugEnabled()){
            log.debug("批量保存的轮播图信息：[{}]",tourTagBannerDTOS);
        }
        tourTagBannerDTOS.stream().peek(tourTagBannerDTO -> {
            tourTagBannerDTO.setCrtTime(Instant.now().toEpochMilli());
            tourTagBannerDTO.setCrtName(userDTO.getUsername());
            tourTagBannerDTO.setIsDel(0);
        }).count();
        return mapper.insertBatch(tourTagBannerDTOS);
    }

    /**
     * 根据标签id查询
     * @param tagId
     * @return
     */
    public List<TourTagBannerDTO> findTourTagBannersByTagId(Integer tagId) {
        List<TourTagBanner> tourTagBanners =  mapper.findByTagId(tagId,0);
        List<TourTagBannerDTO> tourTagBannerDTOS = new ArrayList<>(tourTagBanners.size());
        for (TourTagBanner tourTagBanner : tourTagBanners) {
            TourTagBannerDTO tourTagBannerDTO = new TourTagBannerDTO();
            BeanUtils.copyProperties(tourTagBanner,tourTagBannerDTO);
            tourTagBannerDTOS.add(tourTagBannerDTO);
        }
        return tourTagBannerDTOS;
    }

    /**
     *根据标签id更改banner状态
     * @param tagId
     * @param status
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateTourTagBannerStatusByTagId(Integer tagId, int status) {
      return  mapper.updateTourTagBannerStatusByTagId(tagId,status,Instant.now().toEpochMilli());
    }

    /**
     * 批量更新banner信息
     * @param tourTagBannerDTOS
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateTourBannerByBatch(List<TourTagBannerDTO> tourTagBannerDTOS,Integer tagId,UserDTO userDTO) {
        List<Long> tourTagBannerIds = tourTagBannerDTOS.stream().filter(tourTagBannerDTO -> Objects.nonNull(tourTagBannerDTO.getId())).map(TourTagBannerDTO::getId).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(tourTagBannerIds)){
            mapper.updateTourBannerStatusByTagannerIdsAndTagId(tourTagBannerIds,tagId,1);
        }else {
            if (CollectionUtils.isEmpty(tourTagBannerDTOS)){
                mapper.updateTourTagBannerStatusByTagId(tagId,1,Instant.now().toEpochMilli());
            }
        }
        List<TourTagBannerDTO> tagBannerDTOS = tourTagBannerDTOS.stream().filter(tourTagBannerDTO -> Objects.isNull(tourTagBannerDTO.getId())).collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(tagBannerDTOS)){
            saveBatch(tagBannerDTOS,userDTO);
        }
    }

    /**
     * 更改banner信息
     * @param tourTagBannerDTO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int updateTourBanner(TourTagBannerDTO tourTagBannerDTO,UserDTO userDTO){
         if (log.isDebugEnabled()){
             log.debug("更改的banner信息：[{}]",tourTagBannerDTO);
         }
        tourTagBannerDTO.setUpdName(userDTO.getUsername());
        tourTagBannerDTO.setUpdTime(Instant.now().toEpochMilli());
        return mapper.updateTourTagBanner(tourTagBannerDTO);
    }

    /**
     * 根据标签ids查询
     * @param tagIds
     * @return
     */
    public Map<Integer, List<TourTagBannerDTO>> findTourTagBannersByTagIds(List<Integer> tagIds) {
        List<TourTagBannerDTO> tourTagBannerDTOS =  mapper.findByTagIdsAndStatus(tagIds,0);
        if (log.isDebugEnabled()){
            log.debug("根据标签集合id查询出banner信息:[{}]",tourTagBannerDTOS);
        }
        return tourTagBannerDTOS.stream().collect(Collectors.groupingBy(TourTagBannerDTO::getTagId));
    }

}