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.GovTarget;
import com.nantian.cloud.jljc.service.GovTargetService;
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.web.bind.annotation.*;

import java.util.Date;


/**
 * 国家能耗指标信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/govTarget")
@Api(value = "govTarget",tags ="国家能耗指标信息")
public class GovTargetController {

	private final GovTargetService govTargetService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param govTarget
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取国家能耗指标信息")
	@ApiOperation(value = "分页获取国家能耗指标信息", notes = "分页获取国家能耗指标信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "govTarget", value = "国家能耗指标信息对象（查询条件：指标名称targetName、行业编码industryCode、" +
					"行业名称industryName、产品编码productCode、产品名称productName）", required = false, dataType = "json", paramType = "path")
	})
	public R getGovTargetPage(Page page, GovTarget govTarget) {
		//return R.ok(govTargetService.page(page, Wrappers.query(govTarget)));
		govTarget.setDelFlag("1"); //筛选未删除项
		return R.ok(govTargetService.getPageScope(page, govTarget));
	}


	/**
	 * 信息
	 *
	 * @param govTargetId
	 * @return R
	 */
	@GetMapping("/{govTargetId}")
	@SysLog("根据id获取国家能耗指标信息")
	@ApiOperation(value = "根据id获取国家能耗指标信息", notes = "根据id获取国家能耗指标信息")
	@ApiImplicitParam(name = "govTargetId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("govTargetId") Integer govTargetId) {
		return R.ok(govTargetService.getById(govTargetId));
	}


	/**
	 * 根据节能监察单位产品种类表的type名称获取国家能耗指标信息
	 */
	@GetMapping("/getByTypeName")
	@SysLog("根据节能监察单位产品种类表的type名称获取国家能耗指标信息")
	@ApiOperation(value = "根据节能监察单位产品种类表的type名称获取国家能耗指标信息", notes = "根据节能监察单位产品种类表的type名称获取国家能耗指标信息")
	@ApiImplicitParam(name = "govTargetId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getByTypeName(String name) {
		return R.ok(govTargetService.getByTypeName(name));
	}


	/**
	 * 保存
	 *
	 * @param govTarget
	 * @return R
	 */
	@PostMapping
	@SysLog("新增国家能耗指标信息")
	@ApiOperation(value = "新增国家能耗指标信息", notes = "新增国家能耗指标信息")
	@ApiImplicitParam(name = "GovTarget", value = "国家能耗指标信息对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody GovTarget govTarget) {
		ScUser u= SecurityUtils.getUser();
		govTarget.setUserid(u.getId());
		govTarget.setDeptId(u.getDeptId());
		govTarget.setUpdateTime(new Date());
		govTarget.setDelFlag("1");
		return R.ok(govTargetService.save(govTarget));
	}

	/**
	 * 修改
	 *
	 * @param govTarget
	 * @return R
	 */
	@PutMapping
	@SysLog("修改国家能耗指标信息")
	@ApiOperation(value = "修改国家能耗指标信息", notes = "修改国家能耗指标信息")
	@ApiImplicitParam(name = "GovTarget", value = "国家能耗指标信息对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody GovTarget govTarget) {
		ScUser u= SecurityUtils.getUser();
		govTarget.setUserid(u.getId());
		govTarget.setUpdateTime(new Date());
		return R.ok(govTargetService.updateById(govTarget));
	}

	/**
	 * 删除
	 *
	 * @param govTargetId
	 * @return R
	 */
	@DeleteMapping("/{govTargetId}")
	@SysLog("删除国家能耗指标信息")
	@ApiOperation(value = "删除国家能耗指标信息", notes = "删除国家能耗指标信息")
	@ApiImplicitParam(name = "govTargetId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer govTargetId) {
		GovTarget govTarget = govTargetService.getById(govTargetId);
		if(govTarget==null){
			return R.failed("国家能耗指标信息不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		govTarget.setUserid(u.getId());
		govTarget.setDeptId(u.getDeptId());
		govTarget.setUpdateTime(new Date());
		govTarget.setDelFlag("1");
		return R.ok(govTargetService.removeById(govTargetId));
	}

}
