package com.ym.mec.cms.controller;

import com.ym.mec.cms.controller.queryinfo.NewsInformationQueryInfo;
import com.ym.mec.cms.dal.entity.SysNewsInformation;
import com.ym.mec.cms.service.SysNewsInformationService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("news")
@Api(tags = "资讯服务")
public class NewsController extends BaseController {

	@Autowired
	private SysNewsInformationService sysNewsInformationService;

	@ApiOperation("资讯列表分页查询")
	@GetMapping(value = "/list")
	public Object getList(NewsInformationQueryInfo queryInfo) {
		return succeed(sysNewsInformationService.queryPage(queryInfo));
	}

	@ApiOperation("查询资讯详情")
	@ApiImplicitParam(name = "id", value = "资讯ID编号", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/query/{id}")
	public Object query(@PathVariable("id") Long id) {

		return succeed(sysNewsInformationService.get(id));
	}

	@ApiOperation("新增资讯")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object add(SysNewsInformation newsInfo) {
		return succeed(sysNewsInformationService.insert(newsInfo));
	}

	@ApiOperation("更新资讯")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object update(SysNewsInformation newsInfo) {
		Date date = new Date();
		newsInfo.setUpdateTime(date);

		return succeed(sysNewsInformationService.update(newsInfo));
	}

}
