import logList from "../model/logList.js";

export const sqlSaveLogItem = async (data) => {
    data.isdelete = 1;
    let content = await logList.create(data);
    return content;
}


export const sqlGetListItem = async (id) => {
    let item = await logList.findOne({
        where:{
            id,
            isdelete: 1
        },
        attributes:['contdate','name','content','id']
    });
    let dataValues = item?item.dataValues:"";
    return dataValues;
}

export const sqlGetListWhere = async (data) => {
    let {date = [],name,content,page,size} = data;
    let [startDate,endDate] = date;
    let where = {
        contdate:{
            $gte:startDate,
            $lte:endDate
        },
        name,
        content,
        isdelete:1
    }
    !startDate && !endDate && Reflect.deleteProperty(where,"contdate");
    !name &&  Reflect.deleteProperty(where,"name");
    !content && Reflect.deleteProperty(where,"content");
    let obj = await logList.findAndCountAll({
        attributes:['contdate','name','content','id'],
        where,
        limit:size*1,
        offset:(page*1-1)*size*1,
        order: [['id', 'DESC']]
    });
    let list = obj.rows.map(el => el.dataValues);
    let pageInfo = {count:obj.count,page,size};
    return {list,pageInfo};
}

export const sqlChangeLogsItem = async (data) => {
    let {id,name,contdate,content} = data;
    await logList.update({name,contdate,content},{
        where:{
            id,
            isdelete: 1
        }
    });
    let item = await sqlGetListItem(id);
    return item;
}

export const sqlDeleteItem = async (id) => {
    let items = await logList.update({'isdelete': 0},
        {
            where:{
                id
            }, 
            returning: true
        }
    )
    return items;
}