package com.hotent.base.controller;

import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.hotent.base.enums.ResponseErrorEnums;
import com.hotent.base.manager.BaseManager;
import com.hotent.base.model.CommonResult;
import com.hotent.base.query.PageList;
import com.hotent.base.query.QueryFilter;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 基础的控制器
 *
 * @company 广州宏天软件股份有限公司
 * @author heyifan
 * @email heyf@jee-soft.cn
 * @date 2020年4月6日
 */
public class BaseController2<M extends BaseManager<T>, T extends Model<T>>{
	@Autowired
    protected M baseService;
	
	@PostMapping("/")
	@ApiOperation("添加实体的接口")
	public CommonResult<String> create(@ApiParam(name="model", value="实体信息") @RequestBody T t) {
		boolean result = baseService.save(t);
		if(!result) {
			return CommonResult.<String>result(ResponseErrorEnums.FAIL_OPTION).message("添加实体失败！");
		}
		return CommonResult.<String>ok();
	}

	@PostMapping(value="/query", produces={"application/json; charset=utf-8" })
	@ApiOperation("分页查询结果")
	public CommonResult<PageList<T>> query(@ApiParam(name="queryFilter", value="分页查询信息") @RequestBody QueryFilter<T> queryFilter) {
		PageList<T> list = baseService.query(queryFilter);
		return CommonResult.<PageList<T>>ok().value(list);
	}

	@GetMapping("/{id}")
	@ApiOperation("根据id查询实体")
	public CommonResult<T> getById(@ApiParam(name="id", value="实体id") @PathVariable String id) {
		T entity = baseService.getById(id);
		return CommonResult.<T>ok().value(entity);
	}

	@PutMapping("/")
	@ApiOperation("更新实体")
	public CommonResult<String> updateById(@ApiParam(name="model", value="实体信息") @RequestBody T t) {
		boolean result = baseService.updateById(t);
		if(!result) {
			return CommonResult.<String>result(ResponseErrorEnums.FAIL_OPTION).message("更新实体失败");
		}
		return CommonResult.<String>ok();
	}

	@DeleteMapping("/{id}")
	@ApiOperation("根据id删除")
	public CommonResult<String> deleteById(@ApiParam(name="id", value="实体id") @PathVariable String id) {
		boolean result = baseService.removeById(id);
		if(!result) {
			return CommonResult.<String>result(ResponseErrorEnums.FAIL_OPTION).message("删除实体失败");
		}
		return CommonResult.<String>ok();
	}
	
	@DeleteMapping("/")
	@ApiOperation("根据id集合批量删除")
	public CommonResult<String> deleteByIds(@ApiParam(name="ids", value="实体集合") @RequestParam String...ids) {
		boolean result = baseService.removeByIds(Arrays.asList(ids));
		if(!result) {
			return CommonResult.<String>result(ResponseErrorEnums.FAIL_OPTION).message("删除实体失败");
		}
		return CommonResult.<String>ok();
	}
}
