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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jeequan.jeepay.core.aop.MethodLog;
import com.jeequan.jeepay.core.constants.ApiCodeEnum;
import com.jeequan.jeepay.core.entity.SysUser;
import com.jeequan.jeepay.core.model.ApiRes;
import com.jeequan.jeepay.db.entity.SysArticle;
import com.jeequan.jeepay.mgr.ctrl.CommonCtrl;
import com.jeequan.jeepay.service.impl.SysArticleService;
import org.apache.commons.lang3.StringUtils;
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;

import java.util.Date;

/**
 * 文章管理类
 *
 * @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_NOTICE_LIST')")
    @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())
                .eq(sysArticle.getArticleId()!=null,SysArticle::getArticleId,sysArticle.getArticleId())
                .like(StringUtils.isNotEmpty(sysArticle.getTitle()),SysArticle::getTitle,sysArticle.getTitle());
        // 时间搜索
        Date[] searchDateRange = sysArticle.buildQueryDateRange();
        wrapper.ge(searchDateRange[0] != null, SysArticle::getPublishTime, searchDateRange[0])
                .le(searchDateRange[1] != null, SysArticle::getPublishTime, searchDateRange[1])
                .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("hasAnyAuthority('ENT_NOTICE_VIEW','ENT_NOTICE_EDIT')")
    @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);
    }

    /**
     * @author: yurong
     * @date: 2022-07-08 14:15
     * @describe: 新建文章  articleType 1：公告
     */
    @MethodLog(remark = "新建文章")
    @PreAuthorize("hasAuthority('ENT_NOTICE_ADD')")
    @RequestMapping(value = "",method = RequestMethod.POST)
    public ApiRes add(){
        SysArticle sysArticle = getObject(SysArticle.class);
        if (sysArticle.getArticleType() == null){
            return ApiRes.customFail("文章类型不明确");
        }
        // 当前登录用户信息
        SysUser sysUser = getCurrentUser().getSysUser();
        sysArticle.setCreatedUid(sysUser.getSysUserId());
        sysArticle.setCreatedBy(sysUser.getRealname());
        boolean save = sysArticleService.save(sysArticle);
        if(!save){
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_CREATE);
        }
        return ApiRes.ok();
    }

    /**
     * @author: yurong
     * @date: 2022-07-08 14:15
     * @describe: 编辑文章  articleType 1：公告
     */
    @MethodLog(remark = "编辑文章")
    @PreAuthorize("hasAuthority('ENT_NOTICE_EDIT')")
    @RequestMapping(value = "/{articleId}",method = RequestMethod.PUT)
    public ApiRes update(@PathVariable("articleId") Long articleId){
        SysArticle sysArticle = getObject(SysArticle.class);
        sysArticle.setArticleId(articleId);
        sysArticle.setUpdatedAt(null);
        boolean update = sysArticleService.updateById(sysArticle);
        if (!update){
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_UPDATE);
        }
        return ApiRes.ok();
    }

    /**
     * @author: yurong
     * @date: 2022-07-08 14:15
     * @describe: 删除文章  articleType 1：公告
     */
    @MethodLog(remark = "删除文章")
    @PreAuthorize("hasAuthority('ENT_NOTICE_DEL')")
    @RequestMapping(value = "/{articleId}",method = RequestMethod.DELETE)
    public ApiRes delete(@PathVariable("articleId") Long articleId){
        boolean delete = sysArticleService.removeById(articleId);
        if (!delete){
            return ApiRes.fail(ApiCodeEnum.SYS_OPERATION_FAIL_DELETE);
        }
        return ApiRes.ok();
    }
}

