#!/usr/bin/env python
# -*- coding:utf-8 -*-
from base import BaseModel{% for resource in resources[:1] %}
from {{resource.model_dir}}.schema.{{resource.name}}_schema import {{resource.schema_name}}

class {{resource.model_name}}(BaseModel, {{resource.schema_name}}):

    @classmethod
    def _parse_data(cls, doc, **kwargs):
        """
        组装、修正单条记录
        该方法若存在，框架会自动使用
        若不存在，则模式使用框架内置方法
        :param doc: 单条数据
        :param kwargs:
        :return:
        """
        if not doc:
            return {}

        if "_id" in doc:
            doc["{{resource.name}}_id"] = doc.pop("_id", "")

        return doc

    @classmethod
    def get_{{resource.name}}(cls, {{resource.name}}_id, **kwargs):
        """
        详情
        :return:
        :rtype:
        """
        return cls.find_one_by_oid({{resource.name}}_id)


    @classmethod
    def get_{{resource.name}}_by_spec(cls, spec, **kwargs):
        """
        详情
        :return:
        :rtype:
        """
        return cls.find_one(spec)


    @classmethod
    def get_{{resource.name}}_list_by_last_id(cls, last_id, count, **kwargs):
        """
        前端列表
        :return:
        :rtype:
        """
        spec = {}
        spec.update(kwargs.get("spec", {}))
        data_list, last_id = cls.find_list_by_last_id(spec,
                                                      count,
                                                      sort=[("create_at", -1), ],
                                                      fields=kwargs.get("fields", None),
                                                      last_id_field=False)
        return data_list, last_id

    @classmethod
    def get_{{resource.name}}_list_by_page(cls, page, count, **kwargs):
        """
        后台列表
        :return:
        :rtype:
        """
        spec = {}
        spec.update(kwargs.get("spec", {}))
        data_list, pages, counts = cls.find_list_by_page(spec,
                                                         page,
                                                         count,
                                                         sort=[("create_at", -1), ],
                                                         fields=kwargs.get("fields", None))
        return data_list, pages, counts

    @classmethod
    def create_{{resource.name}}(cls, *args, **kwargs):
        """
        创建
        :return:
        :rtype:
        """
        pass

    @classmethod
    def modify_{{resource.name}}(cls, *args, **kwargs):
        """
        修改
        :return:
        :rtype:
        """
        pass

    @classmethod
    def delete_{{resource.name}}(cls, {{resource.name}}_id, **kwargs):
        """
        删除
        :return:
        :rtype:
        """
        return cls.update({"_id": {{resource.name}}_id}, {"state": -1})




{% endfor -%}