package com.huiju.estateagents.property.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.TpShopType;
import com.huiju.estateagents.property.service.ITpShopTypeService;
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.List;

/**
 * <p>
 * 商铺类型 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-09
 */
@RestController
@RequestMapping("/")

@Api(value = "商铺类型API", description = "商铺类型API")
public class TpShopTypeController extends BaseController {
	
	@Autowired
	private ITpShopTypeService shopTypeService;
	
	@ApiOperation(value = "获取商铺配置列表", notes = "获取商铺配置列表")
	@ApiImplicitParams({
			@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 = "/shop/types",method = RequestMethod.GET)
	public ResponseBean getTypesList(@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 = shopTypeService.getTypesList(userElement.getOrgId(),pageNum,pageSize);
		return responseBean;
	}
	
	@ApiOperation(value = "添加商铺类型", notes = "添加商铺类型")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShopType", name = "shopType", value = "商铺类型"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/add/type",method = RequestMethod.POST)
	public ResponseBean saveShopType(@RequestBody TpShopType shopType, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		shopType.setCommunityId(userElement.getOrgId());
		shopType.setSource("self");
		shopType.setCreateUser(userElement.getUserId());
		shopType.setCreateDate(LocalDateTime.now());
		//查看索引是否存在
		QueryWrapper<TpShopType> shopTypeQueryWrapper = new QueryWrapper<>();
		shopTypeQueryWrapper.eq("community_id",shopType.getCommunityId());
		shopTypeQueryWrapper.eq("sort",shopType.getSort());
		List<TpShopType> list = shopTypeService.list(shopTypeQueryWrapper);
		if (list.size() > 0){
			responseBean.addError("此索引已存在");
			return responseBean;
		}
		boolean state  = shopTypeService.save(shopType);
		if (state){
			responseBean.addSuccess("添加成功");
		}else{
			responseBean.addError("添加失败");
		}
		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/type/{id}",method = RequestMethod.GET)
	public ResponseBean getShopType(@PathVariable Integer id, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		TpShopType tpShopType = shopTypeService.getById(id);
		responseBean.addSuccess(tpShopType);
		return responseBean;
	}
	
	@ApiOperation(value = "更新商铺类型", notes = "更新商铺类型")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataType = "TpShopType", name = "shopType", value = "商铺类型"),
			@ApiImplicitParam(paramType = "header", dataTypeClass = String.class, name = "X-Auth-Token", value = "Token")
	})
	@RequestMapping(value = "/shop/update/type",method = RequestMethod.POST)
	public ResponseBean updateShopType(@RequestBody TpShopType shopType, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		//查看索引是否存在
		QueryWrapper<TpShopType> shopTypeQueryWrapper = new QueryWrapper<>();
		shopTypeQueryWrapper.eq("community_id",userElement.getOrgId());
		shopTypeQueryWrapper.eq("sort",shopType.getSort());
		shopTypeQueryWrapper.ne("id",shopType.getId());
		List<TpShopType> list = shopTypeService.list(shopTypeQueryWrapper);
		if (list.size() > 0){
			responseBean.addError("此索引已存在");
			return responseBean;
		}
		
		boolean state  = shopTypeService.updateById(shopType);
		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/delete/type",method = RequestMethod.DELETE)
	public ResponseBean deleteShopType(@RequestBody String jsonString, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		responseBean = shopTypeService.deleteShopType(jsonString);
		return responseBean;
	}
	
	@ApiOperation(value = "获取商铺配置列表", notes = "获取商铺配置列表")
	@ApiImplicitParams({
			@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 = "/shop/type/select",method = RequestMethod.GET)
	public ResponseBean getTypesSelect(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                       @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize, HttpServletRequest request){
		ResponseBean responseBean = new ResponseBean();
		TaUser userElement = getTaUser(request);
		QueryWrapper<TpShopType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("community_id",userElement.getOrgId());
		queryWrapper.orderByAsc("sort");
		queryWrapper.orderByAsc("create_date");
		responseBean.addSuccess(shopTypeService.list(queryWrapper));
		return responseBean;
	}
}
