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

function getModelManagementList(opts) {
    return async function (ctx, next) {

        const models = ctx.fs.dc.models;
        const { page, limit, modelType } = ctx.query;
        const Op = ctx.fs.dc.ORM.Op;
        let errMsg = { message: '获取模型失败' }
        try {
            let searchWhere = {}
            let option = {
                where: searchWhere,
                order: [["id", "desc"]],
            }

            if (modelType) {
                searchWhere.modelType = modelType;
            }

            option.where = searchWhere
            let total = await models.MetaModel.findAll(option);
            let limit_ = limit || 10;
            let page_ = page || 1;
            let offset = (page_ - 1) * limit_;
            if (limit && page) {
                option.limit = limit_
                option.offset = offset
            }

            const res = await models.MetaModel.findAndCount(option);
            ctx.status = 200;
            ctx.body = res;
        } catch (error) {
            ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
            ctx.status = 400;
            ctx.body = errMsg
        }
    }
}

// 新增模型
function addModelManagement(opts) {
    return async function (ctx, next) {

        const models = ctx.fs.dc.models;
        try {
            const { attributeName, attributeCode, modelType } = ctx.request.body
            const checkName = await models.MetaModel.findOne({ where: { attributeName, modelType } });
            const checkCode = await models.MetaModel.findOne({ where: { attributeCode, modelType } });
            if (checkName || checkCode) {
                ctx.status = 400;
                ctx.body = { message: checkName ? '该属性名称已存在' : "该属性代码已存在" }
            } else {
                let rslt = ctx.request.body;
                await models.MetaModel.create(Object.assign({}, rslt))
                ctx.status = 204;
                ctx.body = { message: '新建模型成功' }
            }

        } catch (error) {
            ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
            ctx.status = 400;
            ctx.body = { message: '新建模型失败' }
        }
    }
}

// 修改模型
function editModelManagement(opts) {
    return async function (ctx, next) {

        try {
            const models = ctx.fs.dc.models;
            const { id } = ctx.params;
            const body = ctx.request.body;
            let modelFind = await models.MetaModel.findOne({ where: { id: id } });
            const { attributeName, attributeCode } = ctx.request.body;
            const { modelType } = modelFind;
            const checkName = await models.MetaModel.findOne({ where: { id: { $not: id }, attributeName, modelType } });
            const checkCode = await models.MetaModel.findOne({ where: { id: { $not: id }, attributeCode, modelType } });
            if (checkName || checkCode) {
                ctx.status = 400;
                ctx.body = { message: checkName ? '该属性名称已存在' : "该属性代码已存在" }
            } else {
                await models.MetaModel.update(
                    body,
                    { where: { id: id, } }
                )
                ctx.status = 204;
                ctx.body = { message: '修改模型成功' }
            }


        } catch (error) {
            ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
            ctx.status = 400;
            ctx.body = { message: '修改模型失败' }
        }
    }
}

// 删除模型
function deleteModelManagement(opts) {
    return async function (ctx, next) {

        try {
            const models = ctx.fs.dc.models;
            const { id } = ctx.params;
            await models.MetaModel.destroy({
                where: {
                    id: id
                }
            })
            ctx.status = 204;
            ctx.body = { message: '删除模型成功' }
        } catch (error) {
            ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`);
            ctx.status = 400;
            ctx.body = { message: '删除模型失败' }
        }
    }
}



module.exports = {
    getModelManagementList,
    addModelManagement,
    editModelManagement,
    deleteModelManagement,

}