| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace app\api\service;
- use app\BaseService;
- use app\common\model\AdminModel;
- use app\common\model\CustomerModel;
- use app\common\model\CustomerZueCoinModel;
- use app\common\model\OrderAnnualFeeModel;
- use app\common\model\OrderModel;
- use app\common\model\OrderProductModel;
- use app\common\model\StoreModel;
- use app\common\model\ZueCoinRecordModel;
- class CustomerService extends BaseService
- {
- private $customerModel;
- private $adminModel;
- private $storeModel;
- private $orderModel;
- private $orderProductModel;
- private $orderAnnualFeeModel;
- private $customerZueCoinModel;
- private $zueCoinRecordModel;
- public function __construct()
- {
- $this->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);
- }
- }
|