package com.nantian.cloud.jly.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.jly.entity.TradeCategory;
import com.nantian.cloud.jly.service.TradeCategoryService;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.security.annotation.Inner;
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.web.bind.annotation.*;

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


/**
 * 行业归类表
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/tradeCategory")
@Api(value = "tradeCategory",tags = "行业归类表")
public class TradeCategoryController {
	private final TradeCategoryService tradeCategoryService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param tradeCategory 请假审批单
	 * @return
	 */
	@Inner(value = false)
	@ApiOperation(value = "查询行业归类表",notes = "条件查询行业归类表")
	@ApiImplicitParams({
			@ApiImplicitParam(name ="CheckPersons",value = "行业归类表",required = false,dataType = "json",paramType = "path"),
			@ApiImplicitParam(name ="Page",value = "分页对象",required = false,dataType = "json",paramType = "path")
	})
	@GetMapping("/page")
	public R getTradeCategoryPage(Page page, TradeCategory tradeCategory) {
		tradeCategory.setDelFlag("1"); //查询未删除数据
		return R.ok(tradeCategoryService.page(page, Wrappers.query(tradeCategory)));
	}


	/**
	 * 信息
	 *
	 * @param tradeCategoryId
	 * @return R
	 */
	@ApiOperation(value = "通过ID查询行业归类表",notes = "通过ID条件查询行业归类表")
	@ApiImplicitParam(name ="tradeCategoryId",value = "行业归类表id",required = true,dataType = "int",paramType = "path")
	@GetMapping("/{tradeCategoryId}")
	public R getById(@PathVariable("tradeCategoryId") Integer tradeCategoryId) {
		return R.ok(tradeCategoryService.getById(tradeCategoryId));
	}

	/**
	 * 保存
	 *
	 * @param tradeCategory
	 * @return R
	 */
	@ApiOperation(value = "通过ID查询行业归类表",notes = "通过ID条件查询行业归类表")
	@ApiImplicitParam(name ="TradeCategoryId",value = "行业归类表id",required = true,dataType = "json",paramType = "path")
	@PostMapping
	public R save(@RequestBody TradeCategory tradeCategory) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			tradeCategory.setUserid(u.getId());
		}
		if (u.getDeptId() != null) {
			tradeCategory.setDeptId(u.getDeptId());
		}
		tradeCategory.setDelFlag("1");
		tradeCategory.setUpdateTime(new Date());
		return R.ok(tradeCategoryService.save(tradeCategory));
	}

	/**
	 * 修改
	 *
	 * @param tradeCategory
	 * @return R
	 */
	@ApiOperation(value = "修改行业归类表",notes = "修改行业归类表")
	@ApiImplicitParam(name ="TradeCategory",value = "行业归类表",required = true,dataType = "json",paramType = "path")
	@PutMapping
	public R updateById(@RequestBody TradeCategory tradeCategory) {
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			tradeCategory.setUserid(u.getId());
		}
		tradeCategory.setUpdateTime(new Date());
		return R.ok(tradeCategoryService.updateById(tradeCategory));
	}

	/**
	 * 删除
	 *
	 * @param tradeCategoryId
	 * @return R
	 */
	@ApiOperation(value = "删除行业归类表",notes = "删除行业归类表")
	@ApiImplicitParam(name ="tradeCategoryId",value = "行业归类表id",required = true,dataType = "int",paramType = "path")
	@DeleteMapping("/{tradeCategoryId}")
	public R removeById(@PathVariable("tradeCategoryId") Integer tradeCategoryId) {
		TradeCategory tradeCategory = tradeCategoryService.getById(tradeCategoryId);
		ScUser u= SecurityUtils.getUser();
		if(u.getId()!=null){
			tradeCategory.setUserid(u.getId());
		}
		tradeCategory.setDelFlag("4");
		tradeCategory.setUpdateTime(new Date());
		return R.ok(tradeCategoryService.updateById(tradeCategory));
	}



}
