CustomerService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\service;
  3. use app\BaseService;
  4. use app\common\model\AdminModel;
  5. use app\common\model\CustomerModel;
  6. use app\common\model\CustomerZueCoinModel;
  7. use app\common\model\OrderAnnualFeeModel;
  8. use app\common\model\OrderModel;
  9. use app\common\model\OrderProductModel;
  10. use app\common\model\StoreModel;
  11. use app\common\model\ZueCoinRecordModel;
  12. class CustomerService extends BaseService
  13. {
  14. private $customerModel;
  15. private $adminModel;
  16. private $storeModel;
  17. private $orderModel;
  18. private $orderProductModel;
  19. private $orderAnnualFeeModel;
  20. private $customerZueCoinModel;
  21. private $zueCoinRecordModel;
  22. public function __construct()
  23. {
  24. $this->customerModel = new CustomerModel();
  25. $this->adminModel = new AdminModel();
  26. $this->storeModel = new StoreModel();
  27. $this->orderModel = new OrderModel();
  28. $this->orderAnnualFeeModel = new OrderAnnualFeeModel();
  29. $this->orderProductModel = new OrderProductModel();
  30. $this->customerZueCoinModel = new CustomerZueCoinModel();
  31. $this->zueCoinRecordModel = new ZueCoinRecordModel();
  32. }
  33. public function add(array $params) {
  34. $isExist = $this->customerModel->doesItExist($params['mobile']);
  35. if ($isExist)
  36. return $this->fail(lang("Fail to add. Data duplication"));
  37. if(isset($params['follow_user_id'])) {
  38. $follow_user = $this->adminModel->findById($params['follow_user_id']);
  39. if (!$follow_user)
  40. return $this->fail(lang("The employees dont exist"));
  41. $params['follow_username'] = $follow_user->nickname;
  42. }
  43. if(isset($params['store_id'])) {
  44. $store = $this->storeModel->findById($params['store_id']);
  45. if(!$store)
  46. return $this->fail(lang("Stores do not exist"));
  47. $params['store_abbr'] = $store->abbr;
  48. }
  49. $params['create_user_id'] =$params['admin_id'];
  50. $params['create_username'] = $params['admin_name'];
  51. $params['create_time'] = time();
  52. $params['update_time'] = time();
  53. $customer = $this->customerModel->create($params);
  54. if(isset($params['zue_coin']) && $params['zue_coin'] > 0) {
  55. if (!isset($params['lose_time']))
  56. return $this->fail(lang("Please enter the Zue Coin expiration time"));
  57. $zueCoin = $this->customerZueCoinModel->create([
  58. 'customer_id' => $customer->id,
  59. 'zue_coin' => $params['zue_coin'],
  60. 'lose_time' => strtotime($params['lose_time']),
  61. 'create_time' => time(),
  62. 'update_time' => time()
  63. ]);
  64. $this->zueCoinRecordModel->save([
  65. 'zue_coin_id' => $zueCoin->id,
  66. 'origin' => 0,
  67. 'change' => $params['zue_coin'],
  68. 'after' => $params['zue_coin'],
  69. 'reason' => 2,
  70. 'create_time' => time(),
  71. 'update_time' => time()
  72. ]);
  73. $customer['zue_coin'] = $zueCoin;
  74. }
  75. return $this->ok($customer->id);
  76. }
  77. public function getCustomer($customer_id = null, $name = null, $mobile = null) {
  78. if($customer_id > 0) {
  79. $customer = $this->customerModel->findById($customer_id);
  80. } else if ($name) {
  81. $customer = $this->customerModel->findByName($name);
  82. } else {
  83. $customer = $this->customerModel->findByMobile($mobile);
  84. }
  85. if (!$customer) return $this->fail(lang('No customer found'));
  86. $orders = $this->orderModel->fetchOrderByCustomerId($customer->id);
  87. //订单总额
  88. $total_receivable_amount = 0;
  89. //订单总数
  90. $total_order_num = count($orders);
  91. //待收取定金
  92. $total_wait_gather_earnest_amount = 0;
  93. //已上传报告数
  94. $total_upload_report = 0;
  95. //未上传报告数
  96. $total_wait_upload_report = 0;
  97. $order_ids = [];
  98. foreach ($orders as $order) {
  99. array_push($order_ids, $order['id']);
  100. $total_receivable_amount += $order['receivable_amount'];
  101. $total_wait_gather_earnest_amount += $order['imposed_amount'];
  102. }
  103. $order_annuals = $this->orderAnnualFeeModel->fetchByOrderIds($order_ids);
  104. // 待收取年费
  105. $total_wait_gather_annual_fee = array_reduce($order_annuals, function ($result, $item) {
  106. $result += $item['fee'];
  107. return $result;
  108. },0);
  109. $customer['total'] = [
  110. 'total_receivable_amount' => $total_receivable_amount,
  111. 'total_order_num' => $total_order_num,
  112. 'total_wait_gather_annual_fee' => $total_wait_gather_annual_fee,
  113. 'total_wait_gather_earnest_amount' => $total_wait_gather_earnest_amount,
  114. 'total_wait_upload_report' => "{$this->orderProductModel->countByCustomerId($customer_id,1,1)}/{$this->orderProductModel->countByCustomerId($customer_id,1)}"
  115. ];
  116. return $this->ok($customer);
  117. }
  118. }