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.CompanyType;
import com.nantian.cloud.jljc.service.CompanyTypeService;
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("/companyType")
@Api(value = "companyType",tags = "企业类别信息")
public class CompanyTypeController {
	private final CompanyTypeService companyTypeService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param companyType
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取企业类别信息")
	@ApiOperation(value = "分页获取企业类别信息", notes = "分页获取企业类别信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "choozeEnterprise", value = "企业类别信息（查询条件：企业类别typeName、耗能下限值minUnit、耗能上限值maxUnit）", required = false, dataType = "json", paramType = "path")
	})
	public R getCompanyTypePage(Page page, CompanyType companyType) {
		//return R.ok(companyTypeService.page(page, Wrappers.query(companyType)));
		companyType.setDelFlag("1"); //筛选未删除项
		return R.ok(companyTypeService.getPageScope(page, companyType));
	}


	/**
	 * 信息
	 *
	 * @param companyTypeId
	 * @return R
	 */
	@GetMapping("/{companyTypeId}")
	@SysLog("根据id获取企业类别信息")
	@ApiOperation(value = "根据id获取企业类别信息", notes = "根据id获取企业类别信息")
	@ApiImplicitParam(name = "companyTypeId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("companyTypeId") Integer companyTypeId) {
		return R.ok(companyTypeService.getById(companyTypeId));
	}

	/**
	 * 保存
	 *
	 * @param companyType
	 * @return R
	 */
	@PostMapping
	@SysLog("新增企业类别信息")
	@ApiOperation(value = "新增企业类别信息", notes = "新增企业类别信息")
	@ApiImplicitParam(name = "CompanyType", value = "企业类别信息对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody CompanyType companyType) {
		ScUser u= SecurityUtils.getUser();
		companyType.setUserid(u.getId());
		companyType.setDeptId(u.getDeptId());
		companyType.setUpdateTime(new Date());
		companyType.setDelFlag("1");
		return R.ok(companyTypeService.save(companyType));
	}

	/**
	 * 修改
	 *
	 * @param companyType
	 * @return R
	 */
	@PutMapping
	@SysLog("修改企业类别信息")
	@ApiOperation(value = "修改企业类别信息", notes = "修改企业类别信息")
	@ApiImplicitParam(name = "CompanyType", value = "企业类别信息对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody CompanyType companyType) {
		ScUser u= SecurityUtils.getUser();
		companyType.setUserid(u.getId());
		companyType.setUpdateTime(new Date());
		return R.ok(companyTypeService.updateById(companyType));
	}

	/**
	 * 删除
	 *
	 * @param companyTypeId
	 * @return R
	 */
	@DeleteMapping("/{companyTypeId}")
	@SysLog("删除企业类别信息")
	@ApiOperation(value = "删除企业类别信息", notes = "删除企业类别信息")
	@ApiImplicitParam(name = "companyTypeId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer companyTypeId) {
		CompanyType companyType = companyTypeService.getById(companyTypeId);
		if(companyType==null){
			return R.failed("企业类别信息不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		companyType.setUserid(u.getId());
		companyType.setUpdateTime(new Date());
		companyType.setDelFlag("4");
		return R.ok(companyTypeService.updateById(companyType));
	}

}
