customerModel = new CustomerModel(); $this->adminModel = new AdminModel(); $this->storeModel = new StoreModel(); $this->orderModel = new OrderModel(); $this->orderAnnualFeeModel = new OrderAnnualFeeModel(); $this->orderProductModel = new OrderProductModel(); $this->customerZueCoinModel = new CustomerZueCoinModel(); $this->zueCoinRecordModel = new ZueCoinRecordModel(); } public function add(array $params) { $isExist = $this->customerModel->doesItExist($params['mobile']); if ($isExist) return $this->fail(lang("Fail to add. Data duplication")); if(isset($params['follow_user_id'])) { $follow_user = $this->adminModel->findById($params['follow_user_id']); if (!$follow_user) return $this->fail(lang("The employees dont exist")); $params['follow_username'] = $follow_user->nickname; } if(isset($params['store_id'])) { $store = $this->storeModel->findById($params['store_id']); if(!$store) return $this->fail(lang("Stores do not exist")); $params['store_abbr'] = $store->abbr; } $params['create_user_id'] =$params['admin_id']; $params['create_username'] = $params['admin_name']; $params['create_time'] = time(); $params['update_time'] = time(); $customer = $this->customerModel->create($params); if(isset($params['zue_coin']) && $params['zue_coin'] > 0) { if (!isset($params['lose_time'])) return $this->fail(lang("Please enter the Zue Coin expiration time")); $zueCoin = $this->customerZueCoinModel->create([ 'customer_id' => $customer->id, 'zue_coin' => $params['zue_coin'], 'lose_time' => strtotime($params['lose_time']), 'create_time' => time(), 'update_time' => time() ]); $this->zueCoinRecordModel->save([ 'zue_coin_id' => $zueCoin->id, 'origin' => 0, 'change' => $params['zue_coin'], 'after' => $params['zue_coin'], 'reason' => 2, 'create_time' => time(), 'update_time' => time() ]); $customer['zue_coin'] = $zueCoin; } return $this->ok($customer->id); } public function getCustomer($customer_id = null, $name = null, $mobile = null) { if($customer_id > 0) { $customer = $this->customerModel->findById($customer_id); } else if ($name) { $customer = $this->customerModel->findByName($name); } else { $customer = $this->customerModel->findByMobile($mobile); } if (!$customer) return $this->fail(lang('No customer found')); $orders = $this->orderModel->fetchOrderByCustomerId($customer->id); //订单总额 $total_receivable_amount = 0; //订单总数 $total_order_num = count($orders); //待收取定金 $total_wait_gather_earnest_amount = 0; //已上传报告数 $total_upload_report = 0; //未上传报告数 $total_wait_upload_report = 0; $order_ids = []; foreach ($orders as $order) { array_push($order_ids, $order['id']); $total_receivable_amount += $order['receivable_amount']; $total_wait_gather_earnest_amount += $order['imposed_amount']; } $order_annuals = $this->orderAnnualFeeModel->fetchByOrderIds($order_ids); // 待收取年费 $total_wait_gather_annual_fee = array_reduce($order_annuals, function ($result, $item) { $result += $item['fee']; return $result; },0); $customer['total'] = [ 'total_receivable_amount' => $total_receivable_amount, 'total_order_num' => $total_order_num, 'total_wait_gather_annual_fee' => $total_wait_gather_annual_fee, 'total_wait_gather_earnest_amount' => $total_wait_gather_earnest_amount, 'total_wait_upload_report' => "{$this->orderProductModel->countByCustomerId($customer_id,1,1)}/{$this->orderProductModel->countByCustomerId($customer_id,1)}" ]; return $this->ok($customer); } public function search($admin_id, $store_id, $text = null) { return $this->customerModel->search($store_id, $text); } }