package org.jeecg.modules.orderplatform.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.orderplatform.entity.PlatformScript;
import org.jeecg.modules.orderplatform.service.IPlatformScriptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 下单平台关联脚本
 */
@Slf4j
@RestController
@RequestMapping("/platformScript")
public class PlatformScriptController {
	@Autowired
	private IPlatformScriptService platformScriptService;

	/**
	 * 分页列表查询
	 */
	@GetMapping(value = "/list")
	public Result<PageInfo<JSONObject>> queryPageList(PlatformScript platformScript,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize
													  ) {
		Result<PageInfo<JSONObject>> result = new Result<>();
		PageInfo<JSONObject> pageList = platformScriptService.page(platformScript.getProjectId(),platformScript.getName(),platformScript.getCreator(),pageNo,pageSize);
		result.setSuccess(true);
		result.setResult(pageList);
		return result;
	}

	/**
	 *   添加
	 */
	@PostMapping(value = "/add")
	public Result<PlatformScript> add(@RequestBody PlatformScript platformScript) {
		Result<PlatformScript> result = new Result<>();
		try {
			platformScriptService.save(platformScript);
			result.success("添加成功！");
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result.error500("操作失败");
		}
		return result;
	}

	/**
	 *  编辑
	 */
	@PostMapping(value = "/edit")
	public Result<PlatformScript> edit(@RequestBody PlatformScript platformScript) {
		Result<PlatformScript> result = new Result<PlatformScript>();
		PlatformScript platformScriptEntity = platformScriptService.getById(platformScript.getId());
		if(platformScriptEntity==null) {
			result.error500("未找到对应实体");
		}else {
			boolean ok = platformScriptService.updateById(platformScript);
			if(ok) {
				result.success("修改成功!");
			}
		}

		return result;
	}

	/**
	 * 通过id查询
	 */
	@GetMapping(value = "/queryById")
	public Result<PlatformScript> queryById(@RequestParam(name="id",required=true) String id) {
		Result<PlatformScript> result = new Result<>();
		PlatformScript platformScript = platformScriptService.getById(id);
		if(platformScript==null) {
			result.error500("未找到对应实体");
		}else {
			result.setResult(platformScript);
			result.setSuccess(true);
		}
		return result;
	}

}
