package com.ym.mec.cms.controller;

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

import java.util.Date;
import java.util.List;

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.dal.entity.HelpCenterCatalog;
import com.ym.mec.cms.service.HelpCenterCatalogService;
import com.ym.mec.cms.service.HelpCenterContentService;
import com.ym.mec.common.controller.BaseController;

@RestController
@RequestMapping("helpCenterCatalog")
@Api(tags = "帮助中心分类")
public class HelpCenterCatalogController extends BaseController {

	@Autowired
	private HelpCenterCatalogService helpCenterCatalogService;

	@Autowired
	private HelpCenterContentService helpCenterContentService;

	@ApiOperation("分类列表分页查询")
	@GetMapping(value = "list")
	@ApiImplicitParams({ @ApiImplicitParam(name = "parentId", value = "父分类编号（可为空）", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "status", value = "状态(1，显示 0，隐藏；可为空)", required = true, dataType = "Integer") })
	public Object list(Integer parentId, Integer status) {
		List<HelpCenterCatalog> dataList = helpCenterCatalogService.queryByParentId(parentId, status);
		return succeed(dataList);
	}

	@ApiOperation("分类详情查询")
	@GetMapping(value = "get")
	@ApiImplicitParam(name = "id", value = "分类编号", required = true, dataType = "Integer", paramType = "path")
	public Object findHelpCatalogById(Integer id) {
		if (id == null || id <= 0)
			return failed("请检查输入的ID");
		return succeed(helpCenterCatalogService.get(id));
	}

	@ApiOperation("修改分类")
	@PostMapping(value = "modify", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object modifyHelpCatalog(HelpCenterCatalog helpCatalog) {
		if (helpCatalog == null)
			return failed("参数输入有误");
		if (helpCatalog.getId() != null && helpCatalog.getId() > 0) {
			helpCatalog.setModifyOn(new Date());
			return helpCenterCatalogService.updateCatalog(helpCatalog) ? succeed() : failed("修改失败");
		} else {
			if (StringUtils.isBlank(helpCatalog.getText())) {
				return failed("帮助分类不能为空");
			}
			helpCatalog.setCreateOn(new Date());
			return helpCenterCatalogService.insert(helpCatalog) > 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 delHelpCatalog(Integer id) {
		if (id == null || id <= 0)
			return failed("ID解析失败");
		List<HelpCenterCatalog> list = helpCenterCatalogService.queryByParentId(id, null);
		if (list != null && list.size() > 0) {
			return failed("删除失败,请先删除子集分类");
		}
		return helpCenterCatalogService.deleteCatalog(id) ? succeed() : failed("删除失败");
	}
}
