/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package test.car.web;


import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import test.car.entity.Car;
import test.car.service.CarService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 薪酬调整申请Controller
 * @author liugf
 * @version 2020-02-09
 */
@RestController
@RequestMapping(value = "/Car/testCar")
public class CarController extends BaseController {

	@Autowired
	private CarService carService;

	@ModelAttribute
	public Car get(@RequestParam(required=false) String id) {
		Car entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = carService.get(id);
		}
		if (entity == null){
			entity = new Car();
		}
		return entity;
	}

	/**
	 * 薪酬调整申请列表数据
	 */
	@GetMapping("list")
	public AjaxJson list(Car car, HttpServletRequest request, HttpServletResponse response) {
		Page<Car> page = carService.findPage(new Page<Car>(request, response), car);
		return AjaxJson.success().put("page",page);
	}

	/**
	 * 根据Id获取薪酬调整申请数据
	 */
	@GetMapping("queryById")
	public AjaxJson queryById(Car car) {
		return AjaxJson.success().put("car", car);
	}

	/**
	 * 保存薪酬调整申请
	 */
	@PostMapping("save")
	public AjaxJson save(Car car, Model model) throws Exception{
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(car);
		if (StringUtils.isNotBlank(errMsg)){
			return AjaxJson.error(errMsg);
		}
		//新增或编辑保存
		carService.save(car);//保存
		return AjaxJson.success("保存薪酬调整申请成功").put("businessTable", "test_activiti_audit").put("car", car.getId());
	}


	/**
	 * 批量删除薪酬调整申请
	 */
	@DeleteMapping("delete")
	public AjaxJson delete(String ids) {
		String idArray[] =ids.split(",");
		for(String id : idArray){
			carService.delete(carService.get(id));
		}
		return AjaxJson.success("删除薪酬调整申请成功");
	}


}