package com.goafanti.news.controller;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.ErrorConstants;
import com.goafanti.common.controller.BaseApiController;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.news.bo.InputJtNews;
import com.goafanti.news.service.EventPlanningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 新闻
 */
@RestController
@RequestMapping(value = "/api/admin/eventPlanning")
public class AdminEventPlanningApiController extends BaseApiController {
	@Autowired
	private EventPlanningService eventPlanningService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Result addNews(InputJtNews in) {
		Result res =new Result();
		inputcheke(in, res);
		if(!res.getError().isEmpty())return res;
		res.data(eventPlanningService.add(in));
		return res;
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public Result update(InputJtNews in) {
		Result res =new Result();
		res.data(eventPlanningService.update(in));
		return res;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Result listNews(InputJtNews in) {
		Result res =new Result();
		res.data(eventPlanningService.listnewsDetails(in));
		return res;
	}

	@RequestMapping(value = "/selectNews", method = RequestMethod.GET)
	public Result selectNews(InputJtNews in) {
		Result res =new Result();
		if(in.getId()==null) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"ID","ID"));
			return res;
		}
		if (!res.getError().isEmpty()) return res;
		res.data(eventPlanningService.selectNews(in));
		return res;
	}

	private Result inputcheke(InputJtNews in, Result res) {
		if(StringUtils.isBlank(in.getContent())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"内容","内容"));
			return res;
		}
		if(StringUtils.isBlank(in.getTitle())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"标题","标题"));
			return res;
		}
		if(StringUtils.isBlank(in.getPublisher())) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"发布人","发布人"));
			return res;
		}
		if(null==in.getType()) {
			res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"类型","类型"));
			return res;
		}
		return res;
	}

	/** 上传图片 **/
	@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
	public Result uploadOrderInvoiceFile(HttpServletRequest req){
		Result res = new Result();
		res.setData(handleFile(res, "/eventPlanning/", false, req, "eventPlanning"));
		return res;
	}

}
