package com.huiju.estateagents.property.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.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.center.taUser.entity.TaUser;
import com.huiju.estateagents.property.common.UserElement;
import com.huiju.estateagents.property.dao.TpShopImgMapper;
import com.huiju.estateagents.property.dao.TpShopMapper;
import com.huiju.estateagents.property.model.TpShop;
import com.huiju.estateagents.property.model.TpShopImg;
import com.huiju.estateagents.property.service.ITpShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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-10
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class TpShopServiceImpl extends ServiceImpl<TpShopMapper, TpShop> implements ITpShopService {

	@Autowired
	private TpShopMapper shopMapper;
	
	@Autowired
	private TpShopImgMapper shopImgMapper;
	
	/**
	 * 根据条件查询商铺列表
	 *
	 * @param id
	 * @param shopName
	 * @param shopTypeId
	 * @param shopStatus
	 * @param userElement
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResponseBean getShopList(Integer id, String shopName, Integer shopTypeId, String shopStatus, TaUser userElement, Integer pageNum, Integer pageSize) {
		ResponseBean responseBean = new ResponseBean();
		Page page = new Page(pageNum,pageSize);
		TpShop tpShop = new TpShop();
		tpShop.setId(id);
		tpShop.setShopName(shopName);
		tpShop.setShopTypeId(shopTypeId);
		tpShop.setShopStatus(shopStatus);
		tpShop.setCommunityId(userElement.getOrgId());
		List<TpShop> list = shopMapper.getShopList(page,tpShop);
		Map<String,Object> map = new HashMap<>();
		map.put("list", list);
		map.put("total", page.getTotal());
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	/**
	 * 保存商铺
	 *
	 * @param shop
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean saveShop(TpShop shop, TaUser userElement) {
		ResponseBean responseBean = new ResponseBean();
		//添加商铺信息
		Integer communityId = userElement.getOrgId();
		LocalDateTime localDateTime = LocalDateTime.now();
		shop.setCommunityId(communityId);
		shop.setCreateUser(userElement.getUserId());
		shop.setCreateDate(localDateTime);
		shop.setUpdateDate(localDateTime);
		shop.setUpdateUser(userElement.getUserId());
		shopMapper.insert(shop);
		//添加商铺app首页图片信息
		TpShopImg appIndexShopImg = new TpShopImg();
		appIndexShopImg.setCommunityId(communityId);
		appIndexShopImg.setCreateDate(localDateTime);
		appIndexShopImg.setImgType("1");
		appIndexShopImg.setShopId(shop.getId());
		appIndexShopImg.setImgUrl(shop.getAppIndexImg());
		shopImgMapper.insert(appIndexShopImg);
		//添加商铺app列表图片信息
		TpShopImg appListShopImg = new TpShopImg();
		appListShopImg.setCommunityId(communityId);
		appListShopImg.setCreateDate(localDateTime);
		appListShopImg.setImgType("2");
		appListShopImg.setShopId(shop.getId());
		appListShopImg.setImgUrl(shop.getAppListImg());
		shopImgMapper.insert(appListShopImg);
		//批量添加app轮播图片
		List<String> list = shop.getAppCarouselImg();
		List<TpShopImg> carouselList = new ArrayList<>();
		list.stream().forEach(e -> {
			TpShopImg appCarouselImg = new TpShopImg();
			appCarouselImg.setCommunityId(communityId);
			appCarouselImg.setCreateDate(localDateTime);
			appCarouselImg.setImgType("3");
			appCarouselImg.setShopId(shop.getId());
			appCarouselImg.setImgUrl(e);
			carouselList.add(appCarouselImg);
		});
		shopImgMapper.batchSave(carouselList);
		responseBean.addSuccess("添加成功");
		return responseBean;
	}
	
	/**
	 * 获取商铺信息
	 *
	 * @param id
	 * @return
	 */
	@Override
	public TpShop getShopById(Integer id) {
		TpShop tpShop = shopMapper.selectById(id);
		//获取商铺app首页图片信息
		QueryWrapper<TpShopImg> tpShopImgQueryWrapper = new QueryWrapper<>();
		tpShopImgQueryWrapper.eq("community_id",tpShop.getCommunityId());
		tpShopImgQueryWrapper.eq("shop_id",tpShop.getId());
		tpShopImgQueryWrapper.eq("img_type","1");
		TpShopImg tpShopImg = shopImgMapper.selectOne(tpShopImgQueryWrapper);
		tpShop.setAppIndexImg(tpShopImg.getImgUrl());
		//获取商铺APP列表图片信息
		QueryWrapper<TpShopImg> tpShopImgListWrapper = new QueryWrapper<>();
		tpShopImgListWrapper.eq("community_id",tpShop.getCommunityId());
		tpShopImgListWrapper.eq("shop_id",tpShop.getId());
		tpShopImgListWrapper.eq("img_type","2");
		TpShopImg listShopImg = shopImgMapper.selectOne(tpShopImgListWrapper);
		tpShop.setAppListImg(listShopImg.getImgUrl());
		//获取商铺轮播图列表
		QueryWrapper<TpShopImg> tpShopImgCarouselWrapper = new QueryWrapper<>();
		tpShopImgCarouselWrapper.eq("community_id",tpShop.getCommunityId());
		tpShopImgCarouselWrapper.eq("shop_id",tpShop.getId());
		tpShopImgCarouselWrapper.eq("img_type","3");
		List<TpShopImg> carouselList = shopImgMapper.selectList(tpShopImgCarouselWrapper);
		List<String> carouselUrlList = new ArrayList<>();
		carouselList.stream().forEach(e -> {
			carouselUrlList.add(e.getImgUrl());
		});
		tpShop.setAppCarouselImg(carouselUrlList);
		return tpShop;
	}
	
	/**
	 * 更新商铺信息
	 *
	 * @param shop
	 * @param userElement
	 * @return
	 */
	@Override
	public ResponseBean updateShop(TpShop shop, TaUser userElement) {
		ResponseBean responseBean = new ResponseBean();
		shopMapper.updateById(shop);
		//添加商铺app首页图片信息
		TpShopImg appIndexShopImg = new TpShopImg();
		appIndexShopImg.setImgUrl(shop.getAppIndexImg());
		QueryWrapper<TpShopImg> tpShopImgQueryWrapper = new QueryWrapper<>();
		tpShopImgQueryWrapper.eq("community_id",shop.getCommunityId());
		tpShopImgQueryWrapper.eq("shop_id",shop.getId());
		tpShopImgQueryWrapper.eq("img_type","1");
		shopImgMapper.update(appIndexShopImg,tpShopImgQueryWrapper);
		//添加商铺app列表图片信息
		TpShopImg appListShopImg = new TpShopImg();
		appListShopImg.setImgUrl(shop.getAppListImg());
		QueryWrapper<TpShopImg> tpShopImgListWrapper = new QueryWrapper<>();
		tpShopImgListWrapper.eq("community_id",shop.getCommunityId());
		tpShopImgListWrapper.eq("shop_id",shop.getId());
		tpShopImgListWrapper.eq("img_type","2");
		shopImgMapper.update(appListShopImg,tpShopImgListWrapper);
		//先删除在批量添加app轮播图片
		QueryWrapper<TpShopImg> tpShopImgCarouselWrapper = new QueryWrapper<>();
		tpShopImgCarouselWrapper.eq("community_id",shop.getCommunityId());
		tpShopImgCarouselWrapper.eq("shop_id",shop.getId());
		tpShopImgCarouselWrapper.eq("img_type","3");
		shopImgMapper.delete(tpShopImgCarouselWrapper);
		//重新添加
		List<String> list = shop.getAppCarouselImg();
		List<TpShopImg> carouselList = new ArrayList<>();
		list.stream().forEach(e -> {
			TpShopImg appCarouselImg = new TpShopImg();
			appCarouselImg.setCommunityId(shop.getCommunityId());
			appCarouselImg.setCreateDate(LocalDateTime.now());
			appCarouselImg.setImgType("3");
			appCarouselImg.setShopId(shop.getId());
			appCarouselImg.setImgUrl(e);
			carouselList.add(appCarouselImg);
		});
		shopImgMapper.batchSave(carouselList);
		responseBean.addSuccess("修改成功");
		return responseBean;
	}
}
