package com.nantian.cloud.jljc.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jljc.entity.TConfig;
import com.nantian.cloud.jljc.service.TConfigService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 配置表
 * @date 2020-10-13
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tConfig")
@Api(value="tConfig",tags = "配置表")
public class TConfigController {
	@Autowired
	private TConfigService tConfigService;
	
	/**
	 * 分页查询配置表
	 */
	@GetMapping("/page")
	@SysLog("分页查询配置表")
	@ApiOperation(value = "分页查询配置表", notes = "分页查询配置表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, paramType = "path"),
			@ApiImplicitParam(name = "tConfig", value = "配置表对象", required = false, paramType = "path")
	})
	public R gettConfigPage(Page page, TConfig tConfig) {
		tConfig.setDelFlag("1"); //查询未删除项
		return R.ok(tConfigService.page(page, Wrappers.query(tConfig)));
	}


	/**
	 * 根据id获取配置表
	 */
	@GetMapping("/getById")
	@SysLog("根据id获取配置表")
	@ApiOperation(value = "根据id获取配置表", notes = "根据id获取配置表")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(Integer id) {
		return R.ok(tConfigService.getById(id));
	}

	/**
	 * 根据source获取配置表
	 */
	@GetMapping("/getBySource")
	@SysLog("根据source获取配置表")
	@ApiOperation(value = "根据source获取配置表", notes = "根据source获取配置表")
	@ApiImplicitParam(name = "source", value = "来源表", required = true, dataType = "string", paramType = "path")
	public R getBySource(String source) {
		TConfig tConfig = new TConfig();
		tConfig.setSource(source);
		return R.ok(tConfigService.getOne(Wrappers.query(tConfig)));
	}


	/**
	 * 新增/修改配置表
	 */
	@PostMapping
	@SysLog("新增/修改配置表")
	@ApiOperation(value = "新增/修改配置表", notes = "新增/修改配置表")
	@ApiImplicitParam(name = "tConfig", value = "配置表对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody TConfig tConfig) {

		//source来源表 信息相同，就修改源信息相同的数据
		TConfig oldConfig = new TConfig();
		oldConfig.setSource(tConfig.getSource());
		TConfig o = tConfigService.getOne(Wrappers.query(oldConfig));
		if(o!=null){
			tConfig.setId(o.getId());
		}

		ScUser u = SecurityUtils.getUser();
		tConfig.setUserid(u.getId());
		tConfig.setUpdateTime(new Date());
		if(tConfig.getId()!=null){
			tConfig.setDelFlag("1");
			tConfig.setDeptId(u.getDeptId());
		}
		return R.ok(tConfigService.saveOrUpdate(tConfig));
	}

	/**
	 * 删除配置表
	 */
	@DeleteMapping("/delById")
	@SysLog("删除配置表")
	@ApiOperation(value = "删除配置表", notes = "删除配置表")
	@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "int", paramType = "path")
	public R delById(Integer id) {
		TConfig tConfig = tConfigService.getById(id);
		ScUser u = SecurityUtils.getUser();
		tConfig.setUserid(u.getId());
		tConfig.setUpdateTime(new Date());
		tConfig.setDelFlag("4");
		return R.ok(tConfigService.updateById(tConfig));
	}

}
