package cn.pluss.platform.controller.systemInfo;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.SystemInfo;
import cn.pluss.platform.systemInfo.SystemInfoService;
import cn.pluss.platform.util.PageUtils;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("systemInfo")
public class SystemInfoController extends BaseNoModelController {

	@Autowired
	private SystemInfoService systemInfoService;

	/**
	 * 引导页
	 * 
	 * @return
	 */
	@GetMapping("/index")
	public String index() {
		return "systemInfo/systemInfoList";
	}

	@GetMapping("/page")
	@ResponseBody
	public Result<PageInfo<SystemInfo>> page(Page<SystemInfo> page,SystemInfo systemInfo){
		Page<SystemInfo> pageData = systemInfoService.PageData(page,systemInfo);
		return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
	}

	@RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
	public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
		if (id != null) {
			SystemInfo systemInfoServiceById = systemInfoService.getById(id);
			request.setAttribute("entity",systemInfoServiceById);
		}
		return "systemInfo/systemInfoBaseReadInfo";
	}

	@PostMapping("/saveOrUpdate")
	@ResponseBody
	public Result<SystemInfo> saveOrUpdate(@RequestBody SystemInfo systemInfo){
		if (systemInfo.getId() == null ){
			systemInfo.setCreateDt(new Date());
			systemInfoService.save(systemInfo);
		}else {
			UpdateWrapper<SystemInfo> systemInfoUpdateWrapper=new UpdateWrapper<SystemInfo>().eq("id",systemInfo.getId());
			systemInfoService.update(systemInfo,systemInfoUpdateWrapper);
		}
		return ResultGenerator.genSuccessResult("保存成功",systemInfo);
	}

	@PostMapping("/delete")
	public Result<SystemInfo> delete(@RequestBody SystemInfo systemInfo){
		systemInfoService.removeById(systemInfo.getId());
		return ResultGenerator.genSuccessResult("删除成功",systemInfo);
	}

}