def convertToDict(self, type=None, **args):

        chat_id = ModelUtils.objectToId(self.chat)
        send_time = ModelUtils.timeToStr(self.send_time)

        # received: Object用户查询好友请求时返回的数据(accepted 为 False)
        if type == "received":
            return {
                'fid': self.id,
                'friend': self.subject.convertToDict('stranger'),
                'send_time': self.send_time
            }

        # send: Subject用户查询好友请求时返回的数据(accepted 为 False)
        if type == "send":
            return {
                'fid': self.id,
                'friend': self.object.convertToDict('stranger'),
                'send_time': self.send_time
            }

        # 已成为好友,获取好友的信息(accepted 为 True)
        if self.accepted:
            uid = args['uid']
            # 如果传入参数(查询玩家的uid)为发起方ID
            if uid == self.subject_id:
                return {
                    'fid': self.id,
                    # 则返回的 friend 字段为目标用户的数据
                    'friend': self.object.convertToDict('friend'),
                    'send_time': send_time,
                    'chat_id': chat_id
                }
            # 如果传入参数(查询玩家的uid)为目标方ID
            elif uid == self.object_id:
                return {
                    'fid': self.id,
                    # 则返回的 friend 字段为发起用户的数据
                    'friend': self.subject.convertToDict('friend'),
                    'send_time': send_time,
                    'chat_id': chat_id
                }
            # 如果都不是
            else:
                return {
                    'fid': self.id,
                    'subject': self.subject.convertToDict('friend'),
                    'object': self.object.convertToDict('friend'),
                    'send_time': send_time,
                    'chat_id': chat_id
                }
        # 未成为好友,获取好友的信息(accepted 为 False)
        else:
            return {
                'fid': self.id,
                'sid': self.subject_id,
                'oid': self.object_id,
                'send_time': send_time
            }
    def convertToDict(self):

        read_time = ModelUtils.timeToStr(self.read_time, None)

        if read_time is not None:
            return {'id': self.id, 'read_time': read_time}

        return {'id': self.id}
    def convertToDict(self, type=None):

        start_date = ModelUtils.timeToStr(self.start_date)
        end_date = ModelUtils.timeToStr(self.end_date)

        if type == "pr":
            prs = self.progressReqs()

            return {
                'task_name': self.name,
                'prs': ModelUtils.objectsToDict(prs)
            }

        if type == "project":
            return {
                'id': self.id,
                'name': self.name,
                'progress': self.progress,
                'level': self.level,
                'status_id': self.status,
                'start_date': start_date,
                'end_date': end_date,
            }

        if type == "detail":
            return self.convertToDict()

        create_time = ModelUtils.timeToStr(self.create_time)

        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'project_id': self.project.id,
            'project_name': self.project.name,
            'parent_id': self.parent.id,
            'parent_name': self.parent.name,
            'status_id': self.status,
            'level': self.level,
            'progress': self.progress,
            'auto_adjust': self.auto_adjust,
            'create_time': create_time,
            'start_date': start_date,
            'end_date': end_date,
        }
    def converToDict(self, type=None):

        create_time = ModelUtils.timeToStr(self.create_time)
        result_time = ModelUtils.timeToStr(self.result_time, None)

        result = {
            'id': self.id,
            'creator_id': self.user_id,
            'creator_name': self.user.name,
            'task_id': self.task_id,
            'progress': self.progress,
            'desc': self.description,
            'create_time': create_time,
        }

        if result_time:
            result['result'] = self.result
            result['result_time'] = result_time

        return result
    def convertToDict(self, type=None):

        # 获取索引信息
        if type == "index":
            return {
                'id': self.id,
                'name': self.name,
            }

        if type == "pr":
            tasks = self.tasks()
            return {'tasks': ModelUtils.objectsToDict(tasks, type='pr')}

        # 汇总信息
        if type == "total":

            sum_tasks, unstart_tasks, started_tasks, \
             finished_tasks, progress = self.analayTasks()

            return {
                'name': "汇总",
                'sum_tasks': sum_tasks,
                'unstart_tasks': unstart_tasks,
                'started_tasks': started_tasks,
                'finished_tasks': finished_tasks,
                'progress': progress / sum_tasks,
                'total': True
            }

        if type == "notices":
            notices = self.notice_set.all()
            creator = self.creator()
            return {
                'id': self.id,
                'name': self.name,
                'creator_id': creator.id,
                'creator_name': creator.name,
                'notices': ModelUtils.objectsToDict(notices)
            }

        # 获取成员视图数据
        if type == "members":

            ann = self.tasks().annotate(
                req_cnt=Count('taskprogress__id', distinct=True))
            agg = ann.aggregate(req_cnt=Sum('req_cnt'))

            members = self.members()

            member_info = ModelUtils.objectsToDict(members,
                                                   type="member_info",
                                                   proj=self)
            member_task = ModelUtils.objectsToDict(members,
                                                   type="member_task",
                                                   proj=self)

            for i in range(members.count()):
                # 玩家是否处于空闲状态 = 进行中的任务数是否为 0
                member_info[i]['is_free'] = \
                 member_task[i]['started_tasks'] == 0
                member_task[i]['rids'] = member_info[i]['rids']

            member_task.append(self.convertToDict('total'))

            return {
                'req_cnt': agg['req_cnt'],
                'member_info': member_info,
                'member_task': member_task,
            }

        chat_id = ModelUtils.objectToId(self.chat)
        create_time = ModelUtils.timeToStr(self.create_time)
        start_date = ModelUtils.timeToStr(self.start_date)
        end_date = ModelUtils.timeToStr(self.end_date)

        res = {
            'id': self.id,
            'name': self.name,
            'type_id': self.type,
            'description': self.description,
            'create_time': create_time,
            'start_date': start_date,
            'end_date': end_date,
            'chat_id': chat_id
        }

        # 获取详情信息(用于详情页展示)
        if type == "detail":

            members = ModelUtils.objectsToDict(self.members(),
                                               type="member",
                                               proj=self)
            tasks = ModelUtils.objectsToDict(self.tasks(True), type="project")

            res['members'] = members
            res['tasks'] = tasks

        return res
    def convertToDict(self, type=None, **args):

        create_time = ModelUtils.timeToStr(self.create_time)

        from project_module.models import Project

        # 获取陌生人的资料
        if type == 'stranger':
            return {
                'id': self.id,
                'username': self.username,
                'gender': int(self.gender),
                'avatar': self.avatar.name,
                'description': self.description,
                'status_id': self.status,
            }

        # 获取队伍成员资料
        if type == 'member' and 'proj' in args:
            proj: Project = args['proj']
            return {
                'id': self.id,
                'name': self.name,
                'contact': self.contact,
                'rids': proj.getRoleIds(self),
            }

        if type == 'member_info' and 'proj' in args:
            proj: Project = args['proj']
            return {
                'id': self.id,
                'name': self.name,
                'gender': int(self.gender),
                'rids': proj.getRoleIds(self),
                'email': self.email,
                'contact': self.contact,
                'status_id': self.status
            }

        if type == 'member_task' and 'proj' in args:
            proj: Project = args['proj']

            member_tasks = proj.getMemberTasks(self)
            sum_tasks, unstart_tasks, started_tasks, finished_tasks, \
             progress = proj.analayTasks(member_tasks)

            return {
                'id': self.id,
                'name': self.name,
                'sum_tasks': sum_tasks,
                'unstart_tasks': unstart_tasks,
                'started_tasks': started_tasks,
                'finished_tasks': finished_tasks,
                'progress': progress / sum_tasks
            }

        # 获取好友的资料
        if type == 'friend':
            return self.convertToDict()

        # 获取好友列表(已接受的)
        if type == 'friends':
            return {
                'friends': ModelUtils.objectsToDict(self.getFriends(),
                                                    uid=self.id)
            }

        # 获取发起好友请求列表
        if type == 'send_reqs':
            return {
                'reqs':
                ModelUtils.objectsToDict(self.getSendFriendReqs(), type='send')
            }
        # 获取发起好友请求列表
        if type == 'received_reqs':
            return {
                'reqs':
                ModelUtils.objectsToDict(self.getReceivedFriendReqs(),
                                         type='received')
            }
        # 个人详细资料
        return {
            'id': self.id,
            'username': self.username,
            'email': self.email,
            'name': self.name,
            'gender': int(self.gender),
            'avatar': self.avatar.name,
            'birth': self.birth,
            'city': self.city,
            'education_id': self.education,
            'duty': self.duty,
            'contact': self.contact,
            'description': self.description,
            'create_time': create_time,
            'status_id': self.status,
        }