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.MeasurementUnit;
import com.nantian.cloud.jljc.service.MeasurementUnitService;
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 lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;


/**
 * 计量单位信息
 *
 * @author
 * @date 2018-09-27 15:20:44
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/measurementUnit")
@Api(value = "measurementUnit",tags = "计量单位信息")
public class MeasurementUnitController {
	private final MeasurementUnitService measurementUnitService;


	/**
	 * 分页查询
	 *
	 * @param page      分页对象
	 * @param measurementUnit
	 * @return
	 */
	@GetMapping("/page")
	@SysLog("分页获取计量单位信息")
	@ApiOperation(value = "分页获取计量单位信息", notes = "分页获取计量单位信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "page", value = "分页", required = false, dataType = "json", paramType = "path"),
			@ApiImplicitParam(name = "measurementUnit", value = "计量单位信息（查询条件：单位类型名称unitTypeName）", required = false, dataType = "json", paramType = "path")
	})
	public R getMeasurementUnitPage(Page page, MeasurementUnit measurementUnit) {
		//return R.ok(measurementUnitService.page(page, Wrappers.query(measurementUnit)));
		measurementUnit.setDelFlag("1");  //筛选未删除项
		return R.ok(measurementUnitService.getPageScope(page, measurementUnit));
	}


	/**
	 * 信息
	 *
	 * @param measurementUnitId
	 * @return R
	 */
	@GetMapping("/{measurementUnitId}")
	@SysLog("根据id获取计量单位信息")
	@ApiOperation(value = "根据id获取计量单位信息", notes = "根据id获取计量单位信息")
	@ApiImplicitParam(name = "measurementUnitId", value = "id", required = true, dataType = "int", paramType = "path")
	public R getById(@PathVariable("measurementUnitId") Integer measurementUnitId) {
		return R.ok(measurementUnitService.getById(measurementUnitId));
	}

	/**
	 * 保存
	 *
	 * @param measurementUnit
	 * @return R
	 */
	@PostMapping
	@SysLog("新增计量单位信息")
	@ApiOperation(value = "新增计量单位信息", notes = "新增计量单位信息")
	@ApiImplicitParam(name = "MeasurementUnit", value = "计量单位信息对象", required = true, dataType = "json", paramType = "path")
	public R save(@RequestBody MeasurementUnit measurementUnit) {
		ScUser u= SecurityUtils.getUser();
		measurementUnit.setUserid(u.getId());
		measurementUnit.setDeptId(u.getDeptId());
		measurementUnit.setUpdateTime(new Date());
		measurementUnit.setDelFlag("1");
		return R.ok(measurementUnitService.save(measurementUnit));
	}

	/**
	 * 修改
	 *
	 * @param measurementUnit
	 * @return R
	 */
	@PutMapping
	@SysLog("修改计量单位信息")
	@ApiOperation(value = "修改计量单位信息", notes = "修改计量单位信息")
	@ApiImplicitParam(name = "MeasurementUnit", value = "计量单位信息对象", required = true, dataType = "json", paramType = "path")
	public R updateById(@RequestBody MeasurementUnit measurementUnit) {
		ScUser u= SecurityUtils.getUser();
		measurementUnit.setUserid(u.getId());
		measurementUnit.setUpdateTime(new Date());
		return R.ok(measurementUnitService.updateById(measurementUnit));
	}

	/**
	 * 删除
	 *
	 * @param measurementUnitId
	 * @return R
	 */
	@DeleteMapping("/{measurementUnitId}")
	@SysLog("删除计量单位信息")
	@ApiOperation(value = "删除计量单位信息", notes = "删除计量单位信息")
	@ApiImplicitParam(name = "measurementUnitId", value = "id", required = true, dataType = "int", paramType = "path")
	public R removeById(@PathVariable Integer measurementUnitId) {
		MeasurementUnit measurementUnit = measurementUnitService.getById(measurementUnitId);
		if(measurementUnit==null){
			return R.failed("计量单位信息不存在，删除失败！");
		}
		ScUser u= SecurityUtils.getUser();
		measurementUnit.setUserid(u.getId());
		measurementUnit.setUpdateTime(new Date());
		measurementUnit.setDelFlag("4");
		return R.ok(measurementUnitService.updateById(measurementUnit));
	}

}
