package com.community.huiju.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.TpShop;
import com.community.huiju.model.TpShopType;
import com.community.huiju.service.ITpShopService;
import com.netflix.ribbon.proxy.annotation.Http;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 小区商铺表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/")
@RefreshScope
@Api(value = "商铺API", description = "商铺API")
public class TpShopController extends BaseController {
	
	@Autowired
	private ITpShopService shopService;
	
	@ApiOperation(value = "根据搜索条件获取角色列表", notes = "根据搜索条件获取角色列表")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "id", value = "商铺编号"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "shopName", value = "商铺名称"),
			@ApiImplicitParam(paramType = "query", dataType = "Integer", name = "shopTypeId", value = "商铺类型"),
			@ApiImplicitParam(paramType = "query", dataType = "String", name = "shopStatus", value = "上架状态"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageNum", value = "分页第几页"),
			@ApiImplicitParam(paramType = "query", dataType = "integer", name = "pageSize", value = "分页每页长度"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token"),
	})
	@RequestMapping(value = "/shops",method = RequestMethod.GET)
	public ResponseBean getShopList(@RequestParam(value = "id") Integer id,
	                                @RequestParam(value = "shopName") String shopName,
	                                @RequestParam(value = "shopTypeId") Integer shopTypeId,
	                                @RequestParam(value = "shopStatus") String shopStatus,
	                                @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
	                                @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
	                                HttpSession session) {
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = shopService.getShopList(id,shopName,shopTypeId,shopStatus,userElement,pageNum,pageSize);
		return responseBean;
	}
	
	@ApiOperation(value = "添加商铺", notes = "添加商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShop", name = "shop", value = "商铺"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/add",method = RequestMethod.POST)
	public ResponseBean saveShop(@RequestBody TpShop shop, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = shopService.saveShop(shop,userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "获取商铺信息", notes = "获取商铺信息")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "商铺Id"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/get/{id}",method = RequestMethod.GET)
	public ResponseBean getShop(@PathVariable Integer id, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		TpShop tpShop = shopService.getShopById(id);
		responseBean.addSuccess(tpShop);
		return responseBean;
	}
	
	@ApiOperation(value = "更新商铺", notes = "更新商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShop", name = "shop", value = "商铺"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/update",method = RequestMethod.POST)
	public ResponseBean updateShop(@RequestBody TpShop shop, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = shopService.updateShop(shop,userElement);
		return responseBean;
	}
	
	@ApiOperation(value = "上架商铺", notes = "上架商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "String", name = "jsonString", value = "商铺id集合"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/shelf",method = RequestMethod.POST)
	public ResponseBean shelfShop(@RequestBody String jsonString, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		JSONArray ids = jsonObject.getJSONArray("idArray");
		List<TpShop> tpShopList = new ArrayList<>();
		for (Object id : ids){
			TpShop tpShop = new TpShop();
			tpShop.setId((Integer) id);
			tpShop.setShopStatus("1");
			tpShop.setUpdateUser(userElement.getId());
			tpShop.setUpdateDate(LocalDateTime.now());
			tpShopList.add(tpShop);
		}
		
		boolean state = shopService.updateBatchById(tpShopList);
		if (state){
			responseBean.addSuccess("上架成功");
		}else{
			responseBean.addError("上架失败");
		}
		return responseBean;
	}
	
	@ApiOperation(value = "下架商铺", notes = "下架商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "String", name = "jsonString", value = "商铺id集合"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/obtained",method = RequestMethod.POST)
	public ResponseBean obtainedShop(@RequestBody String jsonString, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		JSONArray ids = jsonObject.getJSONArray("idArray");
		List<TpShop> tpShopList = new ArrayList<>();
		for (Object id : ids){
			TpShop tpShop = new TpShop();
			tpShop.setId((Integer) id);
			tpShop.setShopStatus("2");
			tpShop.setUpdateUser(userElement.getId());
			tpShop.setUpdateDate(LocalDateTime.now());
			tpShopList.add(tpShop);
		}
		
		boolean state = shopService.updateBatchById(tpShopList);
		if (state){
			responseBean.addSuccess("下架成功");
		}else{
			responseBean.addError("下架失败");
		}
		return responseBean;
	}
}
