'use strict';
const moment = require('moment')



async function getAdvisoryNotices (ctx) {
    try{
        const models = ctx.fs.dc.models
        const { limit, page, name,home } = ctx.query
        let options
        if(home==='true'){
            options  = {
                where: {state:2},
                order: [['publish_time', 'desc']],
                limit: 1,
            }
        }else if(home==='false'){
            options  = {
                where: {state:2},
                order: [['id', 'asc']],
            }
        }else{
            options  = {
                where: {},
                order: [['id', 'asc']],
            }
        }
       
        if (name) {
            options.where.name = { $like: `%${name}%` };
        }
        if (limit) {
            options.limit = Number(limit);
        }
        if (page && limit) {
            options.offset = Number(page) * Number(limit);
        }
        const advisoryNoticeList = await models.AdvisoryNotice.findAndCountAll(options)
        ctx.status = 200;
        ctx.body = advisoryNoticeList
    }catch(error){
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            "message": "获取资讯公告失败"
        }
    }
 }

 async function addOrUpdateAdvisoryNotice(ctx){
    const { id, title,content,attachments } = ctx.request.body
    try{
        if(id){
            // 更新
            await ctx.fs.dc.models.AdvisoryNotice.update({
                title,
                content,
                attachments,
            }, {
                where: {
                    id
                }
            })
            ctx.status = 200;
            ctx.body = {
                message: '更新资讯公告成功'
            }
        }else{
            //新增
            await ctx.fs.dc.models.AdvisoryNotice.create({
                title,
                content,
                attachments,
                state:1
            })
            ctx.status = 200;
            ctx.body = {
                message: '新增资讯公告成功'
            }

        }

    }catch(error){
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            "message": id?'编辑资讯公告失败':'新增资讯公告失败'
        }

    }
 }


 async function delAdvisoryNotice(ctx){
    try{
        const { id } = ctx.params
        await ctx.fs.dc.models.AdvisoryNotice.destroy({
            where: {
                id
            }
        })
        ctx.status = 200;
        ctx.body = {
            message: '删除资讯公告成功'
        }
        
    }catch(error){
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            "message": "删除资讯公告失败"
        }

    }
 }
//上架下架
 async function updateAdvisoryNoticeState(ctx){
    const { id } = ctx.params
    const { msg ,publishTime} = ctx.request.body
    try{
       await ctx.fs.dc.models.AdvisoryNotice.update({
            state:msg==='发布'?2:3,
            publishTime:msg==='发布'?moment().format('YYYY-MM-DD HH:mm:ss'):publishTime,
        },{where:{id}})

        ctx.status = 204

    }catch(error){
        ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
        ctx.status = 400;
        ctx.body = {
            "message": `${msg}公告失败`
        }
    }
 }


module.exports = {
    getAdvisoryNotices,
    addOrUpdateAdvisoryNotice,
    delAdvisoryNotice,
    updateAdvisoryNoticeState

  }