const {request, summary, tags, body, path} = require('koa-swagger-decorator');
import {sqlSaveLogItem,sqlGetListItem,sqlGetListWhere,sqlChangeLogsItem,sqlDeleteItem} from "../repository/logList.js";
import Restful from '../utils/Restful.js';
import {getDate} from "../utils/GetDate.js";

const tag = tags(['表格操作']);

let restful = new Restful();

function setPath(path) {
    return `/list/tabel/${path}`;
}

export default class Routers {
    @request("post",setPath("save/content"))
    @tag
    @summary('添加表格数据')
    @body({
        name:{
            type:"string",
            required:true,
            description:"姓名"
        },
        content:{
            type:"string",
            required:true,
            description:"内容"
        },
        contdate:{
            type:"string",
            required:true,
            description:"日期(yyyy-MM-dd)"
        }
    })
    static async saveLogList (ctx,next){
        let {body} = ctx.request;
        body.contdate = getDate(body.contdate);
        if(!body.contdate){
            restful.custom(ctx,{
                code:400,
                Message:"日期格式不正确"
            })
            return;
        }
        await sqlSaveLogItem(body);
        restful.rest(ctx);
    }

    @request("get",setPath("get/item/{id}"))
    @tag
    @summary('查询单条数据')
    @path({id: { 
        type: 'number', 
        require: true,
        description:"数据id"
    }})
    static async getLogItem(ctx,next){
        let {id} = ctx.params;
        let item = await sqlGetListItem(id);
        item = item?item:"";
        restful.rest(ctx, item);
    }

    @request("post",setPath("get/list/{page}/{size}"))
    @tag
    @summary('查询列表')
    @body({
        date:{
            type:"Array",
            example:["2018-04-01","2019-10-10"],
            description:"日期(yyyy-MM-dd) [开始时间,结束时间]"
        },
        name:{
            type:"string",
            description:"姓名"
        },
        content:{
            type:"string",
            description:"内容"
        }
    })
    @path({
        page:{
            type:"number",
            description:"页码",
            example:1
        },
        size:{
            type:"number",
            description:"数量",
            example:15
        }
    })
    static async getLogList(ctx,next){
        let {body} = ctx.request;
        let {page,size} = ctx.params;
        let {date,name,content} = body;
        let data = await sqlGetListWhere({page,size,date,name,content});
        restful.rest(ctx, data);
    }

    @request("post",setPath("change/item"))
    @tag
    @summary('更改list')
    @body({
        id:{
            type:"number",
            description:"id",
            example:6
        },
        name:{
            type:"string",
            description:"姓名"
        },
        content:{
            type:"string",
            description:"内容"
        },
        contdate:{
            type:"string",
            required:true,
            description:"日期(yyyy-MM-dd)"
        }
    })
    static async changeLogListItem(ctx,next){
        let {body} = ctx.request;
        let item = await sqlChangeLogsItem(body);
        restful.rest(ctx, item);
    }

    @request("delete",setPath("delete/item/{id}"))
    @tag
    @summary('删除数据')
    @path({
        id: { 
            type: 'number', 
            require: true,
            description:"数据id",
            example:6
        }
    })
    static async deleteLogItem(ctx,next){
        let {params} = ctx;
        let {id} = params;
        let items = await sqlDeleteItem(id);
        restful.rest(ctx);
    }
}