ImportCustomController.php 4.3 KB

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