package com.community.huiju.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.dao.TpRentalHouseImgMapper;
import com.community.huiju.dao.TpRentalHouseMapper;
import com.community.huiju.model.TpRentalHouse;
import com.community.huiju.model.TpRentalHouseImg;
import com.community.huiju.service.ITpRentalHouseService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 出租公寓表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-13
 */
@Service
public class TpRentalHouseServiceImpl extends ServiceImpl<TpRentalHouseMapper, TpRentalHouse> implements ITpRentalHouseService {
	
	@Autowired
	private TpRentalHouseMapper tpRentalHouseMapper;
	
	@Autowired
	private TpRentalHouseImgMapper tpRentalHouseImgMapper;
	
	/**
	 * 获取公寓房间列表
	 *
	 * @param id
	 * @param houseName
	 * @param startPrice
	 * @param endPrice
	 * @param rentalType
	 * @param houseType
	 * @param houseStatus
	 * @param userElement
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResponseBean getRentalList(Integer id, String houseName, Integer startPrice, Integer endPrice, String rentalType, String houseType, String houseStatus, UserElement userElement, Integer pageNum, Integer pageSize) {
		ResponseBean responseBean = new ResponseBean();
		Page page = new Page(pageNum,pageSize);
		TpRentalHouse tpRentalHouse = new TpRentalHouse();
		tpRentalHouse.setId(id);
		tpRentalHouse.setHouseName(houseName);
		tpRentalHouse.setRentalType(rentalType);
		tpRentalHouse.setHouseType(houseType);
		tpRentalHouse.setHouseStatus(houseStatus);
		tpRentalHouse.setCommunityId(userElement.getCommunityId());
		tpRentalHouse.setStartPrice(startPrice);
		tpRentalHouse.setEndPrice(endPrice);
		List<TpRentalHouse> list = tpRentalHouseMapper.getRentalList(page,tpRentalHouse);
		Map<String,Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	/**
	 * 保存长租公寓房间信息
	 *
	 * @param rentalHouse
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean saveRental(TpRentalHouse rentalHouse, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		//插入房间信息
		Integer communityId = userElement.getCommunityId();
		Integer userId = userElement.getId();
		LocalDateTime nowTime = LocalDateTime.now();
		rentalHouse.setCommunityId(communityId);
		rentalHouse.setCreateDate(nowTime);
		rentalHouse.setCreateUser(userId);
		rentalHouse.setUpdateDate(nowTime);
		rentalHouse.setUpdateUser(userId);
		tpRentalHouseMapper.insert(rentalHouse);
		//插入公寓首页图信息
		TpRentalHouseImg tpRentalHouseImg = new TpRentalHouseImg();
		tpRentalHouseImg.setCommunityId(communityId);
		tpRentalHouseImg.setCreateDate(nowTime);
		tpRentalHouseImg.setRentalHouseId(rentalHouse.getId());
		tpRentalHouseImg.setImgType("1");
		tpRentalHouseImg.setImgUrl(rentalHouse.getAppListImg());
		tpRentalHouseImgMapper.insert(tpRentalHouseImg);
		//插入公寓轮播图信息
		List<String> imgList = rentalHouse.getAppCarouselImg();
		List<TpRentalHouseImg> rentalHouseImgList = new ArrayList<>();
		imgList.stream().forEach(e -> {
			TpRentalHouseImg rentalHouseImg = new TpRentalHouseImg();
			rentalHouseImg.setCommunityId(communityId);
			rentalHouseImg.setCreateDate(nowTime);
			rentalHouseImg.setRentalHouseId(rentalHouse.getId());
			rentalHouseImg.setImgType("2");
			rentalHouseImg.setImgUrl(e);
			rentalHouseImgList.add(rentalHouseImg);
		});
		tpRentalHouseImgMapper.batchInsert(rentalHouseImgList);
		responseBean.addSuccess("添加成功");
		return responseBean;
	}
	
	/**
	 * 根据ID取值
	 *
	 * @param id
	 * @return
	 */
	@Override
	public TpRentalHouse getRentalById(Integer id) {
		//获取公寓数据
		TpRentalHouse tpRentalHouse = tpRentalHouseMapper.selectById(id);
		//获取公寓列表图片
		QueryWrapper<TpRentalHouseImg> tpRentalHouseImgQueryWrapper = new QueryWrapper<>();
		tpRentalHouseImgQueryWrapper.eq("community_id", tpRentalHouse.getCommunityId());
		tpRentalHouseImgQueryWrapper.eq("rental_house_id",tpRentalHouse.getId());
		tpRentalHouseImgQueryWrapper.eq("img_type","1");
		TpRentalHouseImg tpRentalHouseImg = tpRentalHouseImgMapper.selectOne(tpRentalHouseImgQueryWrapper);
		tpRentalHouse.setAppListImg(tpRentalHouseImg.getImgUrl());
		//获取公寓轮播图图片
		QueryWrapper<TpRentalHouseImg> carouseQueryWrapper = new QueryWrapper<>();
		carouseQueryWrapper.eq("community_id", tpRentalHouse.getCommunityId());
		carouseQueryWrapper.eq("rental_house_id",tpRentalHouse.getId());
		carouseQueryWrapper.eq("img_type","2");
		List<TpRentalHouseImg> tpRentalHouseImgList = tpRentalHouseImgMapper.selectList(carouseQueryWrapper);
		List<String> imgList = new ArrayList<>();
		tpRentalHouseImgList.stream().forEach(e -> {
			imgList.add(e.getImgUrl());
		});
		tpRentalHouse.setAppCarouselImg(imgList);
		return tpRentalHouse;
	}
	
	/**
	 * 更新公寓
	 *
	 * @param rentalHouse
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean updateRental(TpRentalHouse rentalHouse, UserElement userElement) {
		ResponseBean responseBean = new ResponseBean();
		rentalHouse.setUpdateDate(LocalDateTime.now());
		rentalHouse.setUpdateUser(userElement.getId());
		tpRentalHouseMapper.updateById(rentalHouse);
		//更新首页图片
		QueryWrapper<TpRentalHouseImg> tpRentalHouseImgQueryWrapper = new QueryWrapper<>();
		tpRentalHouseImgQueryWrapper.eq("community_id", rentalHouse.getCommunityId());
		tpRentalHouseImgQueryWrapper.eq("rental_house_id",rentalHouse.getId());
		tpRentalHouseImgQueryWrapper.eq("img_type","1");
		TpRentalHouseImg tpRentalHouseImg = new TpRentalHouseImg();
		tpRentalHouseImg.setImgUrl(rentalHouse.getAppListImg());
		tpRentalHouseImgMapper.update(tpRentalHouseImg,tpRentalHouseImgQueryWrapper);
		//更新轮播图，先删除在添加
		QueryWrapper<TpRentalHouseImg> deleteQueryWrapper = new QueryWrapper<>();
		deleteQueryWrapper.eq("community_id", rentalHouse.getCommunityId());
		deleteQueryWrapper.eq("rental_house_id",rentalHouse.getId());
		deleteQueryWrapper.eq("img_type","2");
		tpRentalHouseImgMapper.delete(deleteQueryWrapper);
		//插入公寓轮播图信息
		List<String> imgList = rentalHouse.getAppCarouselImg();
		List<TpRentalHouseImg> rentalHouseImgList = new ArrayList<>();
		imgList.stream().forEach(e -> {
			TpRentalHouseImg rentalHouseImg = new TpRentalHouseImg();
			rentalHouseImg.setCommunityId(rentalHouse.getCommunityId());
			rentalHouseImg.setCreateDate(LocalDateTime.now());
			rentalHouseImg.setRentalHouseId(rentalHouse.getId());
			rentalHouseImg.setImgType("2");
			rentalHouseImg.setImgUrl(e);
			rentalHouseImgList.add(rentalHouseImg);
		});
		tpRentalHouseImgMapper.batchInsert(rentalHouseImgList);
		responseBean.addSuccess("更新成功");
		return responseBean;
	}
}
