package com.huiju.estateagents.property.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.huiju.estateagents.base.BaseController;
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.model.TpShop;
import com.huiju.estateagents.property.service.ITpShopService;
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.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
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("/")

@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 = "分页每页长度"),
	})
	@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,
	                                HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = shopService.getShopList(id,shopName,shopTypeId,shopStatus,userElement,pageNum,pageSize);
		return responseBean;
	}
	
	// @ApiOperation(value = "添加商铺", notes = "添加商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShop", name = "shop", value = "商铺"),

	})
	@RequestMapping(value = "/shop/add",method = RequestMethod.POST)
	public ResponseBean saveShop(@RequestBody TpShop shop, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = shopService.saveShop(shop,userElement);
		return responseBean;
	}
	
	// @ApiOperation(value = "获取商铺信息", notes = "获取商铺信息")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "path", dataType = "Integer", name = "id", value = "商铺Id"),

	})
	@RequestMapping(value = "/shop/get/{id}",method = RequestMethod.GET)
	public ResponseBean getShop(@PathVariable Integer id, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		TpShop tpShop = shopService.getShopById(id);
		responseBean.addSuccess(tpShop);
		return responseBean;
	}
	
	// @ApiOperation(value = "更新商铺", notes = "更新商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShop", name = "shop", value = "商铺"),

	})
	@RequestMapping(value = "/shop/update",method = RequestMethod.POST)
	public ResponseBean updateShop(@RequestBody TpShop shop, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		responseBean = shopService.updateShop(shop,userElement);
		return responseBean;
	}
	
	// @ApiOperation(value = "上架商铺", notes = "上架商铺")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "String", name = "jsonString", value = "商铺id集合"),

	})
	@RequestMapping(value = "/shop/shelf",method = RequestMethod.POST)
	public ResponseBean shelfShop(@RequestBody String jsonString, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		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.getUserId());
			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集合"),

	})
	@RequestMapping(value = "/shop/obtained",method = RequestMethod.POST)
	public ResponseBean obtainedShop(@RequestBody String jsonString, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		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.getUserId());
			tpShop.setUpdateDate(LocalDateTime.now());
			tpShopList.add(tpShop);
		}
		
		boolean state = shopService.updateBatchById(tpShopList);
		if (state){
			responseBean.addSuccess("下架成功");
		}else{
			responseBean.addError("下架失败");
		}
		return responseBean;
	}
}
