package com.community.huiju.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.huiju.dao.TpShopMapper;
import com.community.huiju.dao.TpShopSettingMapper;
import com.community.huiju.dao.TpShopTypeMapper;
import com.community.huiju.model.TpShop;
import com.community.huiju.model.TpShopSetting;
import com.community.huiju.model.TpShopType;
import com.community.huiju.service.ITpShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商铺类型 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2019-05-09
 */
@Service
public class TpShopTypeServiceImpl extends ServiceImpl<TpShopTypeMapper, TpShopType> implements ITpShopTypeService {
	
	@Autowired
	private TpShopTypeMapper shopTypeMapper;

	@Autowired
	private TpShopMapper tpShopMapper;

	@Autowired
	private TpShopSettingMapper shopSettingMapper;
	
	/**
	 * 获取和商铺类型相关的所有数据
	 *
	 * @param communityId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public ResponseBean getTypesList(Integer communityId, Integer pageNum, Integer pageSize) {
		ResponseBean responseBean = new ResponseBean();
		Page page = new Page(pageNum,pageSize);
		//获取列表数据
		QueryWrapper<TpShopType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("community_id",communityId);
		queryWrapper.orderByAsc("sort");
		queryWrapper.orderByAsc("create_date");
		IPage<TpShopType> myPage = shopTypeMapper.selectPage(page,queryWrapper);
		//获取商铺是否app显示
		QueryWrapper<TpShopSetting> settingQueryWrapper = new QueryWrapper<>();
		settingQueryWrapper.eq("community_id",communityId);
		TpShopSetting shopSetting = shopSettingMapper.selectOne(settingQueryWrapper);
		Map<String,Object> map = new HashMap<>();
		map.put("dataList",myPage.getRecords());
		map.put("total",myPage.getTotal());
		map.put("shopSettingValue",null == shopSetting ? false : true);
		responseBean.addSuccess(map);
		return responseBean;
	}
	
	/**
	 * 删除商铺类型
	 *
	 * @param jsonString
	 * @return
	 */
	@Override
	public ResponseBean deleteShopType(String jsonString) {
		ResponseBean responseBean = new ResponseBean();
		StringBuffer message = new StringBuffer();
		message.append("删除成功。");
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		JSONArray ids = jsonObject.getJSONArray("idArray");
		for (Object id : ids){
			//根据ID查询这个数据是否是系统创建，系统创建的不能删除，非系统创建的能删除
			TpShopType shopType = shopTypeMapper.selectById(id.toString());
			if (shopType.getSource().equals("sys")){
				//系统创建不可删除
				message.append(shopType.getTypeName() + "为系统创建，不可删除。");
			}else{
				/* 类型删除时，tp_shop里面shop_type_id应修改为空*/
				QueryWrapper<TpShop>  tpShop= new QueryWrapper<>();
				tpShop.eq("community_id",shopType.getCommunityId());
				tpShop.eq("shop_type_id",id.toString());
				List<TpShop> tpShopList=tpShopMapper.selectList(tpShop);
				for (TpShop shop:tpShopList){
					shop.setShopTypeId(null);
					tpShopMapper.updateById(shop);
				}
				shopTypeMapper.deleteById(id.toString());
			}
		}
		responseBean.addSuccess(message.toString());
		return responseBean;
	}
}
