package cn.superdesk.uniorder.controller.sys.logger;

import cn.superdesk.uniorder.common.json.AnJsonUtils;
import cn.superdesk.uniorder.common.pagination.PageQueryGenericParam;
import cn.superdesk.uniorder.common.pagination.PageQueryParam;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.controller.base.BaseController;
import cn.superdesk.uniorder.dao.entity.sys.logger.SysUserOperationLog;
import cn.superdesk.uniorder.dto.param.system.logger.SysUserOperationLogParam;
import cn.superdesk.uniorder.dto.param.system.logger.SysUserOperationLogQueryCriteria;
import cn.superdesk.uniorder.dubbo.consumer.sys.logger.SysUserOperationLogDubboConsumerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 操作日志
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2020-12-10 17:17:34
 * @UpdateUser: linjinyu
 * @UpdateDate: 2020-12-10 17:17:34
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Api(value = "系统管理-操作日志", tags = {"系统管理-操作日志"})
@Slf4j
@RestController
@RequestMapping("/sysUserOperationLog")
public class SysUserOperationLogController extends BaseController {

	@Autowired
	private SysUserOperationLogDubboConsumerService sysUserOperationLogDubboConsumerService;

	@ApiOperation(value = "分页查询 - 默认不用")
	@RequestMapping(value = "listByObscure", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysUserOperationLog>> pageQuerySysUserOperationLog(@RequestBody PageQueryParam param) {

		log.info("pageQuerySysUserOperationLog:\n{}", AnJsonUtils.toJson(param));

		R<List<SysUserOperationLog>> result = new R<>();

		// TODO 参数合法性判定

		result = sysUserOperationLogDubboConsumerService.pageQuery(param.getConditions(), param.getPageNo(), param.getPageSize());

		return result;
	}

	@ApiOperation(value = "分页查询")
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public R<List<SysUserOperationLog>> pageQueryGenericParamSysUserOperationLog(@RequestBody PageQueryGenericParam<SysUserOperationLogQueryCriteria> param) {

		log.info("pageQuerySysUserOperationLog:\n{}", AnJsonUtils.toJson(param));

		R<List<SysUserOperationLog>> result = new R<>();

		// TODO 参数合法性判定

		result = sysUserOperationLogDubboConsumerService.pageQueryGenericParam(param.getConditions());

		return result;
	}

	@ApiOperation(value = "按id查询")
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public R<List<SysUserOperationLog>> getById(@PathVariable("id") long id) {

		R<List<SysUserOperationLog>> result = new R<>();

		// TODO 参数合法性判定

		SysUserOperationLog entity = sysUserOperationLogDubboConsumerService.findSysUserOperationLogById(id);

		List<SysUserOperationLog> data = new ArrayList<>();
		data.add(entity);

		result.setData(data);

		return result;
	}

	@ApiOperation(value = "新增")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public R<String> addSysUserOperationLog(@RequestBody SysUserOperationLogParam param) {

		log.info("addSysUserOperationLog:\n{}", AnJsonUtils.toJson(param));


		// TODO 参数合法性判定

		sysUserOperationLogDubboConsumerService.addSysUserOperationLog(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public R<String> updateSysUserOperationLog(@RequestBody SysUserOperationLogParam param) {

		log.info("updateSysUserOperationLog:\n{}", AnJsonUtils.toJson(param));

		sysUserOperationLogDubboConsumerService.updateSysUserOperationLog(param);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "删除")
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteSysUserOperationLog(@PathVariable("id") long id) {

		// TODO 参数合法性判定

		sysUserOperationLogDubboConsumerService.deleteSysUserOperationLog(id);

		return new R<>("200", "操作成功", true);
	}

	@ApiOperation(value = "批量删除")
	@RequestMapping(value = "delete_ids", method = RequestMethod.POST)
	@ResponseBody
	public R<String> deleteIdsSysUserOperationLog(@RequestParam(value = "ids") long[] ids) {

		log.info(AnJsonUtils.toJson(ids));

		for (long id : ids) {
			sysUserOperationLogDubboConsumerService.deleteSysUserOperationLog(id);
		}

		return new R<>("200", "操作成功", true);

	}


}
