package com.org.hktshop.controller.system;

import com.org.hktshop.Interface.system.IHktNewsArticleService;
import com.org.hktshop.Interface.system.IHktNewsCategoryService;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.controller.BaseController;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.admin.HktAdmin;
import com.org.hktshop.entity.system.HktNewsArticle;
import com.org.hktshop.entity.system.HktNewsCategory;
import com.org.hktshop.utils.HttpCode;
import com.org.hktshop.utils.Ret;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags="文章管理api")
public class HktNewsArticleController extends BaseController {

    @Autowired
    IHktNewsArticleService newsArticleService;

    @Autowired
    IHktNewsCategoryService newsCategoryService;

    @PostMapping("/async/news/updateNewsArticle")
    @ApiOperation(value = "新增/修改文章")
    public Object updateNewsArticle(@RequestBody HktNewsArticle hktNewsArticle, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = newsArticleService.updateArticle(admin,hktNewsArticle);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(),ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/news/deleteNewsArticle")
    @ApiOperation(value = "删除文章")
    public Object deleteHktNewsArticle(@RequestBody HktNewsArticle hktNewsArticle, HttpServletRequest request) {
        try {
            HktAdmin admin = (HktAdmin) getSessionUserId(request);
            Ret ret = newsArticleService.deleteArticle(admin,hktNewsArticle);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(),ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/news/queryNewsArticlePage")
    @ApiOperation(value = "分页 文章")
    public Object queryNewsArticlePage(@RequestBody HktNewsArticle hktNewsArticle) {
        try {
            Page<HktNewsArticle> page = newsArticleService.queryPage(hktNewsArticle);
            return BaseResult.successWithData(page);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/news/queryNewsArticle")
    @ApiOperation(value = "文章详情 查询")
    public Object queryNewsArticle(@RequestBody HktNewsArticle hktNewsArticle) {
        try {
            HktNewsArticle newsArticle = newsArticleService.queryArticle(hktNewsArticle);
            return BaseResult.successWithData(newsArticle);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    @PostMapping("/async/news/updateNewsCategory")
    @ApiOperation(value = "新增/修改 文章分类管理")
    public Object updateNewsCategory(@RequestBody HktNewsCategory hktNewsCategory) {
        try {
            Ret ret = newsCategoryService.updateHktNewsCategory(hktNewsCategory);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/news/deleteNewsCategory")
    @ApiOperation(value = "删除 文章分类管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章分类id", required = true)
    })
    public Object deleteNewsCategory(@RequestBody HktNewsCategory hktNewsCategory) {
        try {
            Ret ret = newsCategoryService.deleteHktNewsCategory(hktNewsCategory);
            if (ret.isFail()) BaseResult.fail(HttpCode.BAD_REQUEST.getCode(), ret.getStr("msg"));
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/async/news/queryNewsCategoryPage")
    @ApiOperation(value = "查询 分页 文章分类管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页", required = true),
            @ApiImplicitParam(name = "pageSize", value = "单页数量", required = true)
    })
    public Object queryNewsCategoryPage(@RequestBody HktNewsCategory hktNewsCategory) {
        try {
            Page<HktNewsCategory> page = newsCategoryService.queryHktNewsCategoryPages(hktNewsCategory);
            return BaseResult.successWithData(page);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }
}

