package com.huiju.estateagents.property.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.TpShopSettingMapper;
import com.huiju.estateagents.property.dao.TpShopTypeMapper;
import com.huiju.estateagents.property.model.TpShopSetting;
import com.huiju.estateagents.property.model.TpShopType;
import com.huiju.estateagents.property.service.ITpShopSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 商铺开关设置 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-09
 */
@Service
public class TpShopSettingServiceImpl extends ServiceImpl<TpShopSettingMapper, TpShopSetting> implements ITpShopSettingService {
	
	@Autowired
	private TpShopSettingMapper shopSettingMapper;
	
	@Autowired
	private TpShopTypeMapper shopTypeMapper;
	
	/**
	 * 改变商户配置
	 *
	 * @param userElement
	 * @param jsonString
	 * @return
	 */
	@Override
	public ResponseBean changeShopSetting(TaUser userElement, String jsonString) {
		ResponseBean responseBean = new ResponseBean();
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		boolean shopSettingValue = jsonObject.getBoolean("shopSettingValue");
		if (shopSettingValue){
			//开启的情况下，setting表插入一条数据并判断存不存在系统创建的四条数据，不存在添加
			TpShopSetting tpShopSetting = new TpShopSetting();
			tpShopSetting.setCommunityId(userElement.getOrgId());
			tpShopSetting.setCreateDate(LocalDateTime.now());
			
			tpShopSetting.setCreateUser(userElement.getUserId());
			shopSettingMapper.insert(tpShopSetting);
			//查看是否存在系统创建的四条数据
			QueryWrapper<TpShopType> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",userElement.getOrgId());
			queryWrapper.eq("source","sys");
			List<TpShopType> list = shopTypeMapper.selectList(queryWrapper);
			if (list.size() < 1){
				//插入数据
				saveShopTypeDate(userElement.getOrgId(),userElement.getUserId());
			}
		}else{
			//删除setting表里面的数据表示关闭
			QueryWrapper<TpShopSetting> queryWrapper = new QueryWrapper<>();
			queryWrapper.eq("community_id",userElement.getOrgId());
			shopSettingMapper.delete(queryWrapper);
		}
		responseBean.addSuccess("success");
		return responseBean;
	}
	
	/**
	 * 插入四条预置数据
	 * @param communityId
	 * @param userId
	 */
	private void saveShopTypeDate(Integer communityId, Integer userId) {
		LocalDateTime localDateTime = LocalDateTime.now();
		
		List<TpShopType> list = new ArrayList<>();
		
		TpShopType tpShopTypeOne = new TpShopType();
		tpShopTypeOne.setCommunityId(communityId);
		tpShopTypeOne.setTypeName("美食小吃");
		tpShopTypeOne.setSort(1);
		tpShopTypeOne.setSource("sys");
		tpShopTypeOne.setCreateDate(localDateTime);
		tpShopTypeOne.setCreateUser(userId);
		list.add(tpShopTypeOne);
		
		TpShopType tpShopTypeTwo = new TpShopType();
		tpShopTypeTwo.setCommunityId(communityId);
		tpShopTypeTwo.setTypeName("购物消费");
		tpShopTypeTwo.setSort(2);
		tpShopTypeTwo.setSource("sys");
		tpShopTypeTwo.setCreateDate(localDateTime);
		tpShopTypeTwo.setCreateUser(userId);
		list.add(tpShopTypeTwo);
		
		TpShopType tpShopTypeThree = new TpShopType();
		tpShopTypeThree.setCommunityId(communityId);
		tpShopTypeThree.setTypeName("休闲娱乐");
		tpShopTypeThree.setSort(3);
		tpShopTypeThree.setSource("sys");
		tpShopTypeThree.setCreateDate(localDateTime);
		tpShopTypeThree.setCreateUser(userId);
		list.add(tpShopTypeThree);
		
		TpShopType tpShopTypeFour = new TpShopType();
		tpShopTypeFour.setCommunityId(communityId);
		tpShopTypeFour.setTypeName("运动健身");
		tpShopTypeFour.setSort(4);
		tpShopTypeFour.setSource("sys");
		tpShopTypeFour.setCreateDate(localDateTime);
		tpShopTypeFour.setCreateUser(userId);
		list.add(tpShopTypeFour);
		//批量插入数据
		shopTypeMapper.insertBatchIds(list);
	}
	
	
	
}
