package com.yunzhi.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhi.marketing.entity.TaApartmentImg;
import com.yunzhi.marketing.entity.TaBuildingImg;
import com.yunzhi.marketing.mapper.TaApartmentImgMapper;
import com.yunzhi.marketing.mapper.TaBuildingImgMapper;
import com.yunzhi.marketing.service.ITaBuildingImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 楼盘图片表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@Service
public class TaBuildingImgServiceImpl extends ServiceImpl<TaBuildingImgMapper, TaBuildingImg> implements ITaBuildingImgService {

    @Autowired
    TaBuildingImgMapper taBuildingImgMapper;

    @Autowired
    TaApartmentImgMapper taApartmentImgMapper;

    @Override
    public int insertBatch(List<TaBuildingImg> imgs, String buildingId) {
        int rows = 0;

        for (TaBuildingImg img: imgs){
            TaBuildingImg Images = new TaBuildingImg();
            Images.setBuildingId(buildingId);
            Images.setImgType(img.getImgType());
            Images.setUrl(img.getUrl());
            Images.setOrderNo(Integer.valueOf(img.getOrderNo()));
            Images.setStatus(0);
            Images.setCreateDate(LocalDateTime.now());
            int row = taBuildingImgMapper.insert(Images);
            rows += row;
        }

        return rows;
    }

    /**
     * 获取户型图的图片集合
     *
     * @param apartMentIds
     * @return
     */
    @Override
    public List<TaBuildingImg> getAparemnetList(List<String> apartMentIds) {
        List<TaApartmentImg> apartmentList = taApartmentImgMapper.selectList(new QueryWrapper<TaApartmentImg>().in("apartment_id", apartMentIds));
        if (apartmentList.size() > 0){
            return taBuildingImgMapper.selectList(new QueryWrapper<TaBuildingImg>().in("img_id",apartmentList.stream().map(TaApartmentImg::getImgId).collect(Collectors.toList())));
        }
        return new ArrayList<>();
    }
}
