package com.yonge.cooleshow.cms.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
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.web.bind.annotation.*;

import com.yonge.cooleshow.cms.controller.queryinfo.HelpCenterContentQueryInfo;
import com.yonge.cooleshow.cms.dal.entity.HelpCenterContent;
import com.yonge.cooleshow.cms.service.HelpCenterContentService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.toolset.base.page.PageInfo;

import javax.validation.Valid;

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

	@Autowired
	private HelpCenterContentService helpCenterContentService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "帮助中心/公告内容列表分页查询", httpMethod="POST", consumes="application/json", produces="application/json")
	@PostMapping(value = "list", consumes="application/json", produces="application/json")
	public Object findHelpContentByCondition(@Valid @RequestBody HelpCenterContentQueryInfo queryInfo) {

		PageInfo<HelpCenterContent> data = helpCenterContentService.queryPage(queryInfo);
		return succeed(data);
	}

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

	@ApiOperation(value = "新增/修改帮助中心/公告内容", httpMethod="POST", consumes="application/json", produces="application/json")
	@PostMapping(value = "modify",consumes="application/json", produces="application/json")
	public Object modifyHelpContent(@Valid @RequestBody HelpCenterContent helpContent) {
		if (helpContent == null) {
			return failed("参数输入有误");
		}
		if (helpContent.getCatalogId() == 2 &&helpContent.getReleaseTime() == null) {
			return failed("发布时间不能为空");
		}
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null || sysUser.getId() == null) {
			return failed("用户信息获取失败");
		}
		helpContent.setReleaseStatus(0);
		if (helpContent.getOrder() == null) {
			helpContent.setOrder(0);
		}
		if (helpContent.getId() != null && helpContent.getId() > 0) {

			HelpCenterContent helpCenterContent = helpCenterContentService.get(helpContent.getId());
			if (helpCenterContent == null) {
				return failed("未找到相关信息");
			}

			if (helpCenterContent.getStatus() == 1) {
				return failed("启用状态下，不能修改");
			}

			helpContent.setStatus(0);
			helpContent.setUpdateBy(sysUser.getId());
			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.setStatus(0);
			helpContent.setUpdateBy(sysUser.getId());
			helpContent.setModifyOn(new Date());
			helpContent.setCreateOn(new Date());
			helpContent.setCreateBy(sysUser.getId());
			return helpCenterContentService.insert(helpContent) > 0 ? succeed() : failed("添加失败");
		}
	}

	@ApiOperation("删除帮助中心/公告内容")
	@PostMapping(value = "delete/{id}")
	@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "Integer", paramType = "path")
	public Object delHelpContent(@PathVariable("id") Long id) {
		if (id == null || id <= 0)
			return failed("ID解析失败");
		HelpCenterContent helpCenterContent = helpCenterContentService.get(id);
		if (helpCenterContent == null) {
			return failed("未找到相关信息");
		}
		if (helpCenterContent.getStatus() == 1) {
			return failed("启用状态下，不能删除");
		}
		return helpCenterContentService.delete(id) > 0 ? succeed() : failed("删除失败");
	}

	@ApiOperation("启用/禁用")
	@PostMapping(value = "status/{id}")
	@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "Integer", paramType = "path")
	public Object statusHelpContent(@PathVariable("id") Long id) {
		if (id == null || id <= 0)
			return failed("ID解析失败");
		HelpCenterContent helpCenterContent = helpCenterContentService.get(id);
		if (helpCenterContent == null) {
			return failed("未找到相关信息");
		}
		return succeed(helpCenterContentService.status(id));
	}


	@ApiOperation("发布公告")
	@PostMapping(value = "release/{id}")
	@ApiImplicitParam(name = "id", value = "编号", required = true, dataType = "Integer", paramType = "path")
	public Object releaseNotice(@PathVariable("id") Long id) {
		if (id == null || id <= 0)
			return failed("ID解析失败");
		HelpCenterContent helpCenterContent = helpCenterContentService.get(id);
		if (helpCenterContent == null) {
			return failed("未找到相关信息");
		}
		return succeed(helpCenterContentService.noticeRelease(helpCenterContent));
	}
}
