package com.jeequan.jeepay.mch.ctrl.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.SysArticle;
import com.jeequan.jeepay.mch.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.SysArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文章管理类
 *
 * @author yurong
 *
 * @date 2022-07-08 14:15
 */
@RestController
@RequestMapping("api/sysArticles")
public class SysArticleController extends CommonCtrl {

    @Autowired  private SysArticleService sysArticleService;

    /**
     * @author: yurong
     * @date: 2022-07-08 14:15
     * @describe: 文章列表 articleType 1：公告
     */
    @PreAuthorize("hasAuthority('ENT_ARTICLE_NOTICEINFO')")
    @RequestMapping(value = "",method = RequestMethod.GET)
    public ApiRes articleList(){
        SysArticle sysArticle = getObject(SysArticle.class);
        if (sysArticle.getArticleType() == null){
            return ApiRes.customFail("文章类型不明确");
        }
        LambdaQueryWrapper<SysArticle> wrapper = SysArticle.gw();
        wrapper.select(SysArticle::getArticleId,SysArticle::getTitle,SysArticle::getSubtitle,SysArticle::getPublisher,SysArticle::getCreatedAt)
                .eq(SysArticle::getArticleType,sysArticle.getArticleType())
                .apply("FIND_IN_SET('"+ CS.SYS_ROLE_TYPE.MCH+"',"+SysArticle.ARTICLE_RANGE+")")
                .orderByDesc(SysArticle::getPublishTime);
        IPage<SysArticle> iPage = sysArticleService.page(getIPage(), wrapper);
        return ApiRes.page(iPage);
    }

    /**
     * @author: yurong
     * @date: 2022-07-08 14:15
     * @describe: 文章详情  articleType 1：公告
     */
    @PreAuthorize("hasAuthority('ENT_ARTICLE_NOTICEINFO')")
    @RequestMapping(value = "/{articleId}",method = RequestMethod.GET)
    public ApiRes detail(@PathVariable("articleId") Long articleId){
        SysArticle sysArticle = sysArticleService.getById(articleId);
        if (sysArticle == null) {
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_SEARCH);
        }
        return ApiRes.ok(sysArticle);
    }
}

