ImportCustomController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace console\controllers;
  3. use biz\shop\classes\ShopClass;
  4. use biz\sj\classes\SjClass;
  5. use bizGhs\admin\classes\AdminClass;
  6. use bizHd\saas\classes\ApplyClass;
  7. use bizHd\saas\services\ApplyService;
  8. use common\components\dict;
  9. use common\components\stringUtil;
  10. use common\components\util;
  11. use yii\console\Controller;
  12. use Yii;
  13. class ImportCustomController extends Controller
  14. {
  15. //批量导客户进去 ssh 20230408
  16. public function actionInit()
  17. {
  18. ini_set('memory_limit', '2045M');
  19. set_time_limit(0);
  20. $str = <<<CUSTOM
  21. 七天花店,17187822032
  22. CUSTOM;
  23. $str = trim($str);
  24. $arr = explode('
  25. ', $str);
  26. if (empty($arr) || is_array($arr) == false) {
  27. util::stop('客户资料有问题');
  28. }
  29. $customList = [];
  30. foreach ($arr as $item) {
  31. $item = trim($item);
  32. $it = explode(',', $item);
  33. $mobile = $it[1] ?? '';
  34. $name = $it[0] ?? '';
  35. if (empty($name)) {
  36. util::stop('有客户名称是空的');
  37. }
  38. if (stringUtil::isMobile($mobile) == false) {
  39. util::stop('有客户手机号有问题');
  40. }
  41. $customList[] = ['name' => $name, 'mobile' => $mobile];
  42. }
  43. $connection = Yii::$app->db;
  44. $transaction = $connection->beginTransaction();
  45. if (getenv('YII_ENV') == 'production') {
  46. $masterShopId = 4892;
  47. } else {
  48. $masterShopId = 36523;
  49. }
  50. $master = ShopClass::getById($masterShopId, true);
  51. if (empty($master)) {
  52. util::stop('没有找到主人的门店');
  53. }
  54. if (empty($customList)) {
  55. util::stop('没有需要添加的客户');
  56. }
  57. try {
  58. foreach ($customList as $info) {
  59. $shopName = $info['name'] ?? '';
  60. $mobile = $info['mobile'] ?? 0;
  61. $post = [];
  62. $post['introSjId'] = $master->sjId ?? 0;
  63. $post['introShopId'] = $master->id ?? 0;
  64. $post['introSjName'] = $master->merchantName ?? '';
  65. $post['introStyle'] = SjClass::STYLE_SUPPLIER;
  66. $post['name'] = $shopName;
  67. $post['mobile'] = $mobile;
  68. if (empty($shopName)) {
  69. $transaction->rollBack();
  70. util::stop('1');
  71. }
  72. if (stringUtil::isMobile($mobile) == false) {
  73. $transaction->rollBack();
  74. util::stop('2');
  75. }
  76. if (stringUtil::getWordNum($shopName) > 8) {
  77. $transaction->rollBack();
  78. util::stop('3');
  79. }
  80. $has = ApplyClass::getByCondition(['mobile' => $mobile]);
  81. if (!empty($has)) {
  82. $transaction->rollBack();
  83. util::stop('4');
  84. }
  85. $hasShop = ShopClass::getByCondition(['mobile' => $mobile]);
  86. if (!empty($hasShop)) {
  87. $transaction->rollBack();
  88. util::stop('5');
  89. }
  90. $adminName = $post['adminName'] ?? '';
  91. $miniOpenId = $post['miniOpenId'] ?? '';
  92. $adminInfo = ['name' => $adminName, 'mobile' => $mobile, 'miniOpenId' => $miniOpenId];
  93. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  94. $admin = AdminClass::replaceAdmin($adminInfo, $fromApp);
  95. $adminId = $admin['id'] ?? 0;
  96. $post['adminId'] = $adminId;
  97. $post['adminName'] = $admin['name'] ?? '';
  98. $post['long'] = isset($post['longitude']) ? $post['longitude'] : '';
  99. $post['lat'] = isset($post['latitude']) ? $post['latitude'] : '';
  100. $post['from'] = ApplyClass::FROM_RETAIL;
  101. $post['style'] = SjClass::STYLE_RETAIL;
  102. //增加或更新申请
  103. $respond = ApplyService::replaceRetail($post);
  104. $id = $respond['id'] ?? 0;
  105. //后继代码,要用到此参数
  106. Yii::$app->params['ptStyle'] = 2;
  107. ApplyService::pass($id, '批发商手动添加');
  108. }
  109. $transaction->commit();
  110. } catch (\Exception $exception) {
  111. $transaction->rollBack();
  112. }
  113. }
  114. }