const { tpl_demo, op } = require("../../middleware/baseModel");

function build_search_where(seach_option) {
  const key = seach_option && seach_option.key;
  const raw = seach_option && seach_option.value;
  if (!key || raw === undefined || raw === null) {
    return {};
  }
  const str = String(raw).trim();
  if (str === "") {
    return {};
  }
  const attr = tpl_demo.rawAttributes[key];
  if (!attr) {
    return { [key]: { [op.like]: `%${str}%` } };
  }
  const type_key = attr.type && attr.type.key;
  if (type_key === "INTEGER" || type_key === "BIGINT") {
    const n = Number(str);
    if (!Number.isNaN(n)) {
      return { [key]: n };
    }
    return {};
  }
  return { [key]: { [op.like]: `%${str}%` } };
}

module.exports = {
  "POST /tpl_demo/page": async (ctx) => {
    const body = ctx.getBody() || {};
    const param = body.param || body;
    const { limit, offset } = ctx.getPageSize();
    const base_where = { is_delete: 0 };
    const search_where = build_search_where(param.seachOption || {});
    const where = { ...base_where, ...search_where };
    const { count, rows } = await tpl_demo.findAndCountAll({
      where,
      offset,
      limit,
      order: [["id", "DESC"]],
    });
    return ctx.success({ rows, count });
  },

  "POST /tpl_demo/add": async (ctx) => {
    const body = ctx.getBody() || {};
    const title = body.title != null ? String(body.title).trim() : "";
    if (!title) {
      return ctx.fail("请填写标题");
    }
    const row = await tpl_demo.create({
      title,
      remark: body.remark != null ? String(body.remark).trim() : "",
    });
    return ctx.success(row);
  },

  "POST /tpl_demo/edit": async (ctx) => {
    const body = ctx.getBody() || {};
    const id = body.id;
    if (!id) {
      return ctx.fail("缺少 id");
    }
    const title = body.title != null ? String(body.title).trim() : "";
    if (!title) {
      return ctx.fail("请填写标题");
    }
    await tpl_demo.update(
      {
        title,
        remark: body.remark != null ? String(body.remark).trim() : "",
        last_modify_time: new Date(),
      },
      { where: { id, is_delete: 0 } }
    );
    return ctx.success({});
  },

  "POST /tpl_demo/del": async (ctx) => {
    const body = ctx.getBody() || {};
    const id = body.id;
    if (!id) {
      return ctx.fail("缺少 id");
    }
    await tpl_demo.update(
      { is_delete: 1, last_modify_time: new Date() },
      { where: { id, is_delete: 0 } }
    );
    return ctx.success({});
  },

  "GET /tpl_demo/detail": async (ctx) => {
    const id = ctx.get("id");
    if (!id) {
      return ctx.fail("缺少 id");
    }
    const row = await tpl_demo.findOne({ where: { id, is_delete: 0 } });
    return ctx.success(row);
  },

  "GET /tpl_demo/all": async (ctx) => {
    const rows = await tpl_demo.findAll({ limit: 500, order: [["id", "DESC"]] });
    return ctx.success(rows);
  },

  "POST /tpl_demo/export": async (ctx) => {
    const body = ctx.getBody() || {};
    const param = body.param || body;
    const search_where = build_search_where(param.seachOption || {});
    const where = { is_delete: 0, ...search_where };
    const rows = await tpl_demo.findAll({ where, limit: 10000, order: [["id", "DESC"]] });
    return ctx.success({ rows });
  },
};
