Browse Source

定制客户模块
查詢定制客户信息、添加定制客户信息、定制客户设备扫码添加接口、查询定制客户已扫码添加设备数量

linhaohong 1 năm trước cách đây
mục cha
commit
6c18535077
2 tập tin đã thay đổi với 229 bổ sung0 xóa
  1. 2 0
      Ansjer/urls.py
  2. 227 0
      Controller/CustomCustomer/CustomCustomerController.py

+ 2 - 0
Ansjer/urls.py

@@ -27,6 +27,7 @@ from Controller import FeedBack, EquipmentOTA, EquipmentInfo, AdminManage, AppIn
     DeviceLogController, CouponController, AiController, ShadowController, AppAccountManagement, InitController, \
     WeatherControl, SmartReplyController, InAppPurchaseController, DeviceCommonController
 from Controller.Cron import CronTaskController
+from Controller.CustomCustomer import CustomCustomerController
 from Controller.MessagePush import EquipmentMessagePush
 from Controller.SensorGateway import SensorGatewayController, EquipmentFamilyController
 from Controller.Surveys import CloudStorageController
@@ -281,6 +282,7 @@ urlpatterns = [
     re_path('inAppPurchase/(?P<operation>.*)', InAppPurchaseController.InAppPurchaseView.as_view()),
     re_path('account/changeAccountInfo/(?P<operation>.*)$', UserController.ChangeAccountInfoView.as_view()),
     re_path('deviceCommon/(?P<operation>.*)$', DeviceCommonController.DeviceCommonView.as_view()),
+    re_path('customCustomer/(?P<operation>.*)$', CustomCustomerController.CustomCustomerView.as_view()),
 
 
 

+ 227 - 0
Controller/CustomCustomer/CustomCustomerController.py

@@ -0,0 +1,227 @@
+import time
+
+from django.core.paginator import Paginator
+from django.views import View
+
+from Model.models import CustomCustomerOrderInfo, CustomCustomerDevice, UidSetModel
+from Object.ResponseObject import ResponseObject
+from Object.TokenObject import TokenObject
+from Service.CommonService import CommonService
+from Ansjer.config import LOGGER
+
+
+class CustomCustomerView(View):
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        request_dict = request.GET
+        return self.validation(request, request_dict, operation)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        request_dict = request.POST
+        return self.validation(request, request_dict, operation)
+
+    def validation(self, request, request_dict, operation):
+        language = request_dict.get('language', 'en')
+        response = ResponseObject(language)
+        tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
+        response.lang = tko.lang
+        if tko.code != 0:
+            return response.json(tko.code)
+        if operation == 'getCustomCustomerList':  # 查詢定制客户信息
+            return self.get_custom_customer_list(request_dict, response)
+        elif operation == 'addCustomCustomer':  # 添加定制客户信息接口
+            return self.add_custom_customer(request_dict, response)
+        elif operation == 'addCustomerDevice':  # 定制客户设备扫码添加接口
+            return self.add_customer_device(request_dict, response)
+        elif operation == 'getCustomerDeviceCount':  # 查询定制客户已扫码添加设备数量接口
+            return self.get_customer_device_count(request_dict, response)
+        else:
+            return response.json(414)
+
+    @staticmethod
+    def get_custom_customer_list(request_dict, response):
+        """
+        查询定制客户信息
+        :param request_dict:
+        :param response:
+        :return:
+        """
+        order_number = request_dict.get('orderNumber', None)
+        name = request_dict.get('name', None)
+        email = request_dict.get('email', None)
+        page = request_dict.get('pageNo', 1)
+        page_size = request_dict.get('pageSize', 10)
+        try:
+            # 初始化查询集
+            custom_customer_qs = CustomCustomerOrderInfo.objects.all()
+
+            if order_number:
+                custom_customer_qs = custom_customer_qs.filter(order_number__icontains=order_number)
+            if name:
+                custom_customer_qs = custom_customer_qs.filter(name__icontains=name)
+            if email:
+                custom_customer_qs = custom_customer_qs.filter(email__icontains=email)
+
+            paginator = Paginator(custom_customer_qs.order_by('id'), page_size)
+            customers = paginator.page(page)
+
+            # Convert the queryset to a list of dictionaries
+            customer_list = []
+            for customer in customers.object_list:
+                customer_list.append({
+                    'cId': customer.id,
+                    'orderNumber': customer.order_number,
+                    'name': customer.name,
+                    'email': customer.email,
+                    'countryId': customer.country_id,
+                    'quantity': customer.quantity,
+                    'createdTime': customer.created_time,
+                })
+
+            # 返回分页结果
+            data = {
+                'total': paginator.count,
+                'list': list(customer_list),  # 当前页的记录列表
+            }
+
+            return response.json(0, data)
+        except Exception as e:
+            LOGGER.info("查询定制客户信息, errLine:{}, errMsg:{}".format(e.__traceback__.tb_lineno, repr(e)))
+            return response.json(500)
+
+    @staticmethod
+    def add_custom_customer(request_dict, response):
+        """
+        添加定制客户信息
+        :param request_dict: 请求字典
+        :param response: 响应对象
+        :return: 响应对象
+        """
+        order_number = request_dict.get('orderNumber', None)
+        name = request_dict.get('name', None)
+        quantity = request_dict.get('quantity', None)
+        email = request_dict.get('email', '')
+        country_id = request_dict.get('countryId', 0)
+
+        if not order_number or not name:
+            return response.json(444, 'orderNumber、name和quantity是必要参数')
+
+        custom_customer_qs = CustomCustomerOrderInfo.objects.filter(order_number=order_number)
+        if custom_customer_qs.exists():
+            return response.json(174, 'orderNumber重复')
+
+        try:
+            CustomCustomerOrderInfo.objects.create(
+                order_number=order_number,
+                name=name,
+                email=email,
+                country_id=country_id,
+                quantity=quantity,
+                created_time=int(time.time()),
+                updated_time=int(time.time())
+            )
+            return response.json(0)
+        except Exception as e:
+            LOGGER.info("添加定制客户信息, errLine:{}, errMsg:{}".format(e.__traceback__.tb_lineno, repr(e)))
+            return response.json(500)
+
+    def add_customer_device(self, request_dict, response):
+        """
+        定制客户设备扫码添加接口
+        :param request_dict:
+        :param response:
+        :return:
+        """
+        # 获取并验证请求中的必要参数
+        c_id = request_dict.get('cId')
+        full_code = request_dict.get('fullCode', '').strip()
+
+        if not c_id or not full_code:
+            return response.json(444, "cId、fullCode是必要参数")
+
+        # 检查full_code的格式并提取相关字段
+        uid, serial_number = self.process_full_code(full_code)
+        if not uid:
+            return response.json(444, 'fullCode格式错误')
+
+        device_type = UidSetModel.objects.filter(uid=uid)[0].device_type
+        # 验证客户信息是否存在
+        custom_customer = CustomCustomerOrderInfo.objects.filter(id=c_id).first()
+        if not custom_customer:
+            return response.json(173)
+
+        # 创建新设备记录
+        try:
+            CustomCustomerDevice.objects.create(
+                c_id=c_id,
+                serial_number=serial_number,
+                uid=uid,
+                full_code=full_code,
+                type=device_type,
+                created_time=int(time.time()),
+                updated_time=int(time.time())
+            )
+            return response.json(0)
+        except Exception as e:
+            LOGGER.info(
+                "定制客户设备扫码添加接口异常, errLine:{}, errMsg:{}".format(e.__traceback__.tb_lineno, repr(e)))
+            return response.json(500)
+
+    @staticmethod
+    def process_full_code(full_code):
+        """ 处理full_code, 返回uid和serial_number """
+        serial_number = ""
+        uid = ""
+        if len(full_code) == 14:
+            serial_number = full_code[:9]
+            device_uid = CommonService.query_uid_with_serial(serial_number)
+            if len(device_uid) == 20:
+                uid = device_uid
+        elif len(full_code) == 20:
+            uid = full_code
+        return uid, serial_number
+
+    @staticmethod
+    def get_customer_device_count(request_dict, response):
+        """
+        查询定制客户已扫码添加设备数量
+        :param request_dict:
+        :param response:
+        :return:
+        """
+        order_number = request_dict.get('orderNumber', None)
+        name = request_dict.get('name', None)
+
+        try:
+            if not order_number and not name:
+                # 如果没有提供 order_number 和 name,返回所有设备的总数
+                device_count = CustomCustomerDevice.objects.count()
+            else:
+                # 初始查询集
+                customer_orders_qs = CustomCustomerOrderInfo.objects.all()
+
+                # 根据 order_number 或 name 过滤
+                if order_number:
+                    customer_orders_qs = customer_orders_qs.filter(order_number=order_number)
+                if name:
+                    customer_orders_qs = customer_orders_qs.filter(name=name)
+
+                # 获取 c_id 列表
+                c_ids = customer_orders_qs.values_list('id', flat=True)
+
+                # 如果没有匹配的订单,返回设备数量为 0
+                if not c_ids:
+                    device_count = 0
+                else:
+                    # 根据 c_id 列表查询设备数量
+                    device_count = CustomCustomerDevice.objects.filter(c_id__in=c_ids).count()
+
+            return response.json(0, {'deviceCount': device_count})
+        except Exception as e:
+            LOGGER.info(
+                "查询定制客户已扫码添加设备数量接口异常, errLine:{}, errMsg:{}".format(e.__traceback__.tb_lineno,
+                                                                                       repr(e)))
+            return response.json(500)