package com.community.huiju.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.community.commom.mode.ResponseBean;
import com.community.commom.session.UserElement;
import com.community.huiju.common.base.BaseController;
import com.community.huiju.model.AnnouncementTel;
import com.community.huiju.model.TpShopType;
import com.community.huiju.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.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.List;

/**
 * <p>
 * 商铺类型 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-09
 */
@RestController
@RequestMapping("/")
@RefreshScope
@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, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		responseBean = shopTypeService.getTypesList(userElement.getCommunityId(),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, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		shopType.setCommunityId(userElement.getCommunityId());
		shopType.setSource("self");
		shopType.setCreateUser(userElement.getId());
		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, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		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, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		//查看索引是否存在
		QueryWrapper<TpShopType> shopTypeQueryWrapper = new QueryWrapper<>();
		shopTypeQueryWrapper.eq("community_id",userElement.getCommunityId());
		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, HttpSession session){
		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, HttpSession session){
		ResponseBean responseBean = new ResponseBean();
		UserElement userElement = getUserElement(session);
		QueryWrapper<TpShopType> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("community_id",userElement.getCommunityId());
		queryWrapper.orderByAsc("sort");
		queryWrapper.orderByAsc("create_date");
		responseBean.addSuccess(shopTypeService.list(queryWrapper));
		return responseBean;
	}
}
