#!/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 get_{{resource.name}}(cls, {{resource.name}}_id, **kwargs):
        """
        详情
        :return:
        :rtype:
        """
        d = cls.find_one_by_oid({{resource.name}}_id)
        return cls._parse_data(d)


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


    @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 = cls.find_list_by_page(spec,
                                                 page,
                                                 count,
                                                 sort=[("create_at", -1), ],
                                                 fields=kwargs.get("fields", None))
        return data_list, pages

    @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})

    @classmethod
    def _parse_data(cls, d, **kwargs):
        """
        组装单条数据
        :return:
        :rtype:
        """
        if not d:
            return {}

        d["{{resource.name}}_id"] = d.pop("_id", "")

        return d


{% endfor -%}