CustomerService.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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\StoreModel;
  8. use app\common\model\ZueCoinRecordModel;
  9. class CustomerService extends BaseService
  10. {
  11. private $customerModel;
  12. private $adminModel;
  13. private $storeModel;
  14. private $customerZueCoinModel;
  15. private $zueCoinRecordModel;
  16. public function __construct()
  17. {
  18. $this->customerModel = new CustomerModel();
  19. $this->adminModel = new AdminModel();
  20. $this->storeModel = new StoreModel();
  21. $this->customerZueCoinModel = new CustomerZueCoinModel();
  22. $this->zueCoinRecordModel = new ZueCoinRecordModel();
  23. }
  24. public function add(array $params) {
  25. $isExist = $this->customerModel->doesItExist($params['mobile']);
  26. if ($isExist)
  27. return $this->fail(lang("Fail to add. Data duplication"));
  28. if(isset($params['follow_user_id'])) {
  29. $follow_user = $this->adminModel->findById($params['follow_user_id']);
  30. if (!$follow_user)
  31. return $this->fail(lang("The employees dont exist"));
  32. $params['follow_username'] = $follow_user->nickname;
  33. }
  34. if(isset($params['store_id'])) {
  35. $store = $this->storeModel->findById($params['store_id']);
  36. if(!$store)
  37. return $this->fail(lang("Stores do not exist"));
  38. $params['store_abbr'] = $store->abbr;
  39. }
  40. $params['create_user_id'] =$params['admin_id'];
  41. $params['create_username'] = $params['admin_name'];
  42. $params['create_time'] = time();
  43. $params['update_time'] = time();
  44. $customer = $this->customerModel->create($params);
  45. if(isset($params['zue_coin']) && $params['zue_coin'] > 0) {
  46. if (!isset($params['lose_time']))
  47. return $this->fail(lang("Please enter the Zue Coin expiration time"));
  48. $zueCoin = $this->customerZueCoinModel->create([
  49. 'customer_id' => $customer->id,
  50. 'zue_coin' => $params['zue_coin'],
  51. 'lose_time' => strtotime($params['lose_time']),
  52. 'create_time' => time(),
  53. 'update_time' => time()
  54. ]);
  55. $this->zueCoinRecordModel->save([
  56. 'zue_coin_id' => $zueCoin->id,
  57. 'origin' => 0,
  58. 'change' => $params['zue_coin'],
  59. 'after' => $params['zue_coin'],
  60. 'reason' => 2,
  61. 'create_time' => time(),
  62. 'update_time' => time()
  63. ]);
  64. $customer['zue_coin'] = $zueCoin;
  65. }
  66. return $this->ok($customer->id);
  67. }
  68. }