102 lines
3.4 KiB
Python
102 lines
3.4 KiB
Python
|
|
from system.models import Contacts, HistorySession
|
||
|
|
from ninja import Router, Schema, Field
|
||
|
|
|
||
|
|
router = Router()
|
||
|
|
|
||
|
|
class addContactSchema(Schema):
|
||
|
|
MMSI: str = Field(None, alias="mmsi")
|
||
|
|
name: str = Field(None, alias="name")
|
||
|
|
phone: str = Field(None, alias="phone")
|
||
|
|
region: str = Field(None, alias="region")
|
||
|
|
|
||
|
|
class deleteContactSchema(Schema):
|
||
|
|
MMSI: str = Field(None, alias="mmsi")
|
||
|
|
|
||
|
|
class editContactSchema(Schema):
|
||
|
|
oldMMSI: str = Field(None, alias="old_mmsi")
|
||
|
|
newMMSI: str = Field(None, alias="new_mmsi")
|
||
|
|
name: str = Field(None, alias="name")
|
||
|
|
phone: str = Field(None, alias="phone")
|
||
|
|
region: str = Field(None, alias="region")
|
||
|
|
|
||
|
|
|
||
|
|
@router.get('/contactsList')
|
||
|
|
def get_contacts_list(request):
|
||
|
|
try:
|
||
|
|
contacts = Contacts.objects.all().values()
|
||
|
|
return {
|
||
|
|
"code": 200,
|
||
|
|
"message": "成功",
|
||
|
|
"data": list(contacts)
|
||
|
|
}
|
||
|
|
except Exception as e:
|
||
|
|
return {'code': 500, 'message': str(e)}
|
||
|
|
|
||
|
|
@router.post('/deleteContact')
|
||
|
|
def delete_contact(request, data: deleteContactSchema):
|
||
|
|
try:
|
||
|
|
if not data.MMSI:
|
||
|
|
return {'code': 400, 'message': 'MMSI不能为空'}
|
||
|
|
contact = Contacts.objects.filter(MMSI=data.MMSI).first()
|
||
|
|
if not contact:
|
||
|
|
return {'code': 404, 'message': '未找到对应联系人'}
|
||
|
|
# 删除关联的历史会话记录
|
||
|
|
HistorySession.objects.filter(FormId=contact.Id).delete()
|
||
|
|
contact.delete()
|
||
|
|
return {'code': 200, 'message': '删除成功'}
|
||
|
|
except Exception as e:
|
||
|
|
return {'code': 500, 'message': str(e)}
|
||
|
|
|
||
|
|
|
||
|
|
@router.post('/editContact')
|
||
|
|
def edit_contact(request, data: editContactSchema):
|
||
|
|
try:
|
||
|
|
if not data.oldMMSI:
|
||
|
|
return {'code': 400, 'message': '原MMSI不能为空'}
|
||
|
|
|
||
|
|
contact = Contacts.objects.filter(MMSI=data.oldMMSI).first()
|
||
|
|
if not contact:
|
||
|
|
return {'code': 404, 'message': '未找到对应联系人'}
|
||
|
|
# 更新联系人信息
|
||
|
|
contact.MMSI = data.newMMSI if data.newMMSI else contact.MMSI
|
||
|
|
contact.Name = data.name if data.name else contact.Name
|
||
|
|
contact.Mobile = data.phone if data.phone else contact.Mobile
|
||
|
|
contact.Region = data.region if data.region else contact.Region
|
||
|
|
contact.save()
|
||
|
|
|
||
|
|
# 更新关联的历史会话记录
|
||
|
|
if data.newMMSI or data.name:
|
||
|
|
HistorySession.objects.filter(FormId=contact.Id).update(
|
||
|
|
MMSI=data.newMMSI if data.newMMSI else contact.MMSI,
|
||
|
|
Name=data.name if data.name else contact.Name
|
||
|
|
)
|
||
|
|
|
||
|
|
return {
|
||
|
|
'code': 200,
|
||
|
|
'message': '修改成功',
|
||
|
|
'data': {
|
||
|
|
'id': contact.Id,
|
||
|
|
'MMSI': contact.MMSI
|
||
|
|
}
|
||
|
|
}
|
||
|
|
except Exception as e:
|
||
|
|
return {'code': 500, 'message': str(e)}
|
||
|
|
|
||
|
|
@router.post('/addContact')
|
||
|
|
def add_contact(request, data: addContactSchema):
|
||
|
|
try:
|
||
|
|
if not data.MMSI:
|
||
|
|
return {'code': 400, 'message': 'MMSI不能为空'}
|
||
|
|
name = data.name if data.name else data.MMSI
|
||
|
|
contact = Contacts.objects.create(
|
||
|
|
MMSI=data.MMSI,
|
||
|
|
Name=name,
|
||
|
|
Mobile=data.phone,
|
||
|
|
Region=data.region
|
||
|
|
)
|
||
|
|
return {
|
||
|
|
'code': 200,
|
||
|
|
'message': '成功',
|
||
|
|
}
|
||
|
|
except Exception as e:
|
||
|
|
return {'code': 500, 'message': str(e)}
|