package com.ym.mec.cms.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.cms.controller.queryinfo.HelpCenterContentQueryInfo;
import com.ym.mec.cms.dal.entity.HelpCenterContent;
import com.ym.mec.cms.service.HelpCenterCatalogService;
import com.ym.mec.cms.service.HelpCenterContentService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.PageInfo;

@RestController
@RequestMapping("helpCenterContent")
@Api(tags = "帮助中心内容")
public class HelpCenterContentControlller extends BaseController {

	@Autowired
	private HelpCenterContentService helpCenterContentService;

	@Autowired
	private HelpCenterCatalogService helpCenterCatalogService;

	@ApiOperation("帮助中心内容列表分页查询")
	@GetMapping(value = "list")
	public Object findHelpContentByCondition(HelpCenterContentQueryInfo queryInfo) {

		queryInfo.setSort("order_");
		queryInfo.setOrder("asc");
		PageInfo<HelpCenterContent> data = helpCenterContentService.queryPage(queryInfo);
		return succeed(data);
	}

	@ApiOperation("帮助中心内容明细查询")
	@GetMapping(value = "get")
	@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "Integer", paramType = "path")
	public Object findHelpContentByKey(Integer id) {
		if (id == null || id <= 0) {
			return failed("请检查输入的参数");
		}
		return succeed(helpCenterContentService.get(id));
	}

	@ApiOperation("修改帮助中心内容")
	@PostMapping(value = "modify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object modifyHelpContent(HelpCenterContent helpContent) {
		if (helpContent == null) {
			return failed("参数输入有误");
		}

		if (helpContent.getId() != null && helpContent.getId() > 0) {
			helpContent.setModifyOn(new Date());
			return helpCenterContentService.update(helpContent) > 0 ? succeed() : failed("修改失败");
		} else {
			if (StringUtils.isBlank(helpContent.getContent())) {
				return failed("帮助内容不能为空");
			}
			if (StringUtils.isBlank(helpContent.getTitle())) {
				return failed("帮助中心标题不能为空");
			}
			if (helpContent.getCatalogId() < 0) {
				return failed("请选择分类");
			}
			helpContent.setCreateOn(new Date());
			return helpCenterContentService.insert(helpContent) > 0 ? succeed() : failed("添加失败");
		}
	}

	@ApiOperation("删除帮助中心内容")
	@PostMapping(value = "delete", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "Integer", paramType = "path")
	public Object delHelpContent(Integer id) {
		if (id == null || id <= 0)
			return failed("ID解析失败");
		return helpCenterContentService.delete(id) > 0 ? succeed() : failed("删除失败");
	}
}
