/*
 * @Author: doramart
 * @Date: 2019-06-24 13:20:49
 * @Last Modified by: doramart
 * @Last Modified time: 2021-04-17 23:12:28
 */

'use strict';
const Service = require('egg').Service;
const path = require('path');

// general是一个公共库，可用可不用
const {
  _list,
  _item,
  _count,
  _create,
  _update,
  _removes,
  _safeDelete,
  _updateMany,
} = require(path.join(process.cwd(), 'app/service/general'));

class ContentTemplateService extends Service {
  async find(
    payload,
    { query = {}, searchKeys = [], populate = [], files = null } = {}
  ) {
    const listdata = _list(this.ctx.model.ContentTemplate, payload, {
      query,
      searchKeys,
      populate,
      files,
    });
    return listdata;
  }

  async count(params = {}) {
    return _count(this.ctx.model.ContentTemplate, params);
  }

  async create(payload) {
    return _create(this.ctx.model.ContentTemplate, payload);
  }

  async removes(res, values, key = '_id') {
    return _removes(res, this.ctx.model.ContentTemplate, values, key);
  }

  async safeDelete(res, values) {
    return _safeDelete(res, this.ctx.model.ContentTemplate, values);
  }

  async removeItems(id, values) {
    return await this.ctx.model.ContentTemplate.findOneAndUpdate(
      {
        _id: id,
      },
      {
        $pull: {
          items: values,
        },
      }
    );
  }

  async addItems(id, values) {
    return await this.ctx.model.ContentTemplate.findOneAndUpdate(
      {
        _id: id,
      },
      {
        $push: {
          items: values,
        },
      }
    );
  }

  async update(res, _id, payload) {
    return _update(res, this.ctx.model.ContentTemplate, _id, payload);
  }

  async updateMany(res, ids, payload, query) {
    return _updateMany(
      res,
      this.ctx.model.ContentTemplate,
      ids,
      payload,
      query
    );
  }

  async item(res, params = {}) {
    return _item(res, this.ctx.model.ContentTemplate, params);
  }
}

module.exports = ContentTemplateService;
