ItemController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. namespace console\controllers;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\item\classes\PtItemClass;
  5. use biz\item\classes\PtItemClassClass;
  6. use biz\sj\classes\SjClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use biz\shop\classes\ShopClass;
  9. use bizGhs\item\classes\ItemClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\order\classes\StockWastageOrderClass;
  12. use bizGhs\order\classes\StockWastageOrderItemClass;
  13. use bizHd\saas\classes\ApplyClass;
  14. use bizHd\saas\models\Apply;
  15. use bizHd\saas\services\ApplyService;
  16. use common\components\dict;
  17. use common\components\imgUtil;
  18. use common\components\stringUtil;
  19. use yii\console\Controller;
  20. use Yii;
  21. use bizGhs\product\classes\ProductClass;
  22. /**
  23. * 花材初始化 ssh 2021.2.18
  24. */
  25. class ItemController extends Controller
  26. {
  27. public $sjId, $shopId, $sj;
  28. //包装纸丝带同步 ssh 20230525
  29. public function actionPaperSync()
  30. {
  31. $connection = Yii::$app->db;
  32. $transaction = $connection->beginTransaction();
  33. try {
  34. $oldItemList = ItemClass::getAllByCondition(['classId' => 25831, 'delStatus' => 0], null, '*');
  35. foreach ($oldItemList as $oldItem) {
  36. unset($oldItem['id']);
  37. $oldItem['classId'] = 31100;
  38. $oldItem['stock'] = 0;
  39. $oldItem['status'] = 2;
  40. $oldItem['mainId'] = 4553;
  41. $oldItem['sjId'] = 16936;
  42. $oldItem['shopId'] = 0;
  43. ItemClass::add($oldItem);
  44. }
  45. $transaction->commit();
  46. } catch (\Exception $e) {
  47. $transaction->rollBack();
  48. $msg = $e->getMessage();
  49. echo "报错了:" . $msg;
  50. }
  51. }
  52. public function actionWaste()
  53. {
  54. ini_set('memory_limit', '2045M');
  55. set_time_limit(0);
  56. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  57. if (!empty($list)) {
  58. foreach ($list as $order) {
  59. $orderSn = $order->orderSn ?? '';
  60. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  61. $totalCost = 0;
  62. if (!empty($itemList)) {
  63. foreach ($itemList as $v) {
  64. $num = $v->itemNum ?? 0;
  65. $itemCost = $v->itemCost ?? 0;
  66. $totalCost = bcadd($totalCost, bcmul($num, $itemCost, 2), 2);
  67. }
  68. }
  69. $order->cost = $totalCost;
  70. $order->save();
  71. }
  72. }
  73. }
  74. public function actionWastage()
  75. {
  76. ini_set('memory_limit', '2045M');
  77. set_time_limit(0);
  78. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  79. if (!empty($list)) {
  80. foreach ($list as $order) {
  81. $orderSn = $order->orderSn ?? '';
  82. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  83. $totalPrice = 0;
  84. if (!empty($itemList)) {
  85. foreach ($itemList as $v) {
  86. $num = $v->itemNum ?? 0;
  87. $itemPrice = $v->itemPrice ?? 0;
  88. $totalPrice = bcadd($totalPrice, bcmul($num, $itemPrice, 2), 2);
  89. }
  90. }
  91. $order->price = $totalPrice;
  92. $order->save();
  93. }
  94. }
  95. }
  96. //初见花季 ssh 20230223
  97. public function actionCj()
  98. {
  99. if (getenv('YII_ENV') == 'production') {
  100. $mtMainId = 3974;
  101. $ttMainId = 53;
  102. } else {
  103. $mtMainId = 703;
  104. $ttMainId = 644;
  105. }
  106. $list = ItemClass::getAllByCondition(['mainId' => $mtMainId], null, '*');
  107. if (!empty($list)) {
  108. echo '已经有花材了';
  109. exit();
  110. }
  111. $classList = ItemClassClass::getAllByCondition(['mainId' => $ttMainId], null, '*', null, true);
  112. if (empty($classList)) {
  113. echo '没有找到天天鲜花的分类';
  114. exit();
  115. }
  116. $connection = Yii::$app->db;
  117. $transaction = $connection->beginTransaction();
  118. try {
  119. foreach ($classList as $class) {
  120. $oldId = $class->id ?? 0;
  121. $cover = $class->cover ?? '';
  122. $name = $class->name ?? '';
  123. $inTurn = $class->inTurn ?? 0;
  124. $isDefault = $class->isDefault ?? 0;
  125. $newClass = [
  126. 'mainId' => $mtMainId,
  127. 'cover' => $cover,
  128. 'name' => $name,
  129. 'inTurn' => $inTurn,
  130. 'isDefault' => $isDefault,
  131. ];
  132. $newRespond = ItemClassClass::add($newClass, true);
  133. $newClassId = $newRespond->id ?? 0;
  134. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  135. foreach ($oldItemList as $oldItem) {
  136. unset($oldItem['id']);
  137. $oldItem['classId'] = $newClassId;
  138. $oldItem['stock'] = 0;
  139. $oldItem['status'] = 2;
  140. $oldItem['mainId'] = $mtMainId;
  141. $oldItem['sjId'] = 16300;
  142. $oldItem['shopId'] = 0;
  143. ItemClass::add($oldItem);
  144. }
  145. }
  146. $transaction->commit();
  147. } catch (\Exception $e) {
  148. $transaction->rollBack();
  149. $msg = $e->getMessage();
  150. echo "报错了:" . $msg;
  151. }
  152. }
  153. //美天鲜花 ssh 20230223
  154. public function actionMt()
  155. {
  156. if (getenv('YII_ENV') == 'production') {
  157. $mtMainId = 3923;
  158. $ttMainId = 742;
  159. } else {
  160. $mtMainId = 703;
  161. $ttMainId = 644;
  162. }
  163. $list = ItemClass::getAllByCondition(['mainId' => $mtMainId], null, '*');
  164. if (!empty($list)) {
  165. echo '已经有花材了';
  166. exit();
  167. }
  168. $classList = ItemClassClass::getAllByCondition(['mainId' => $ttMainId], null, '*', null, true);
  169. if (empty($classList)) {
  170. echo '没有找到天天鲜花的分类';
  171. exit();
  172. }
  173. $connection = Yii::$app->db;
  174. $transaction = $connection->beginTransaction();
  175. try {
  176. foreach ($classList as $class) {
  177. $oldId = $class->id ?? 0;
  178. $cover = $class->cover ?? '';
  179. $name = $class->name ?? '';
  180. $inTurn = $class->inTurn ?? 0;
  181. $isDefault = $class->isDefault ?? 0;
  182. $newClass = [
  183. 'mainId' => $mtMainId,
  184. 'cover' => $cover,
  185. 'name' => $name,
  186. 'inTurn' => $inTurn,
  187. 'isDefault' => $isDefault,
  188. ];
  189. $newRespond = ItemClassClass::add($newClass, true);
  190. $newClassId = $newRespond->id ?? 0;
  191. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  192. foreach ($oldItemList as $oldItem) {
  193. unset($oldItem['id']);
  194. $oldItem['classId'] = $newClassId;
  195. $oldItem['stock'] = 0;
  196. $oldItem['status'] = 2;
  197. $oldItem['mainId'] = $mtMainId;
  198. $oldItem['sjId'] = 16300;
  199. $oldItem['shopId'] = 0;
  200. ItemClass::add($oldItem);
  201. }
  202. }
  203. $transaction->commit();
  204. } catch (\Exception $e) {
  205. $transaction->rollBack();
  206. $msg = $e->getMessage();
  207. echo "报错了:" . $msg;
  208. }
  209. }
  210. //花材反向更新 ./yii item/reverse-update
  211. public function actionReverseUpdate()
  212. {
  213. $shopId = 10;
  214. if (getenv('YII_ENV') != 'production') {
  215. $shopId = 36225;
  216. }
  217. $list = ProductClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
  218. if (empty($list)) {
  219. return false;
  220. }
  221. foreach ($list as $cKey => $product) {
  222. $ptItemId = $product->itemId ?? 0;
  223. $ptItem = PtItemClass::getById($ptItemId, true);
  224. if (empty($ptItem)) {
  225. continue;
  226. }
  227. $ptItem->name = $product->name ?? '';
  228. $ptItem->py = $product->py ?? '';
  229. $ptItem->cover = $product->cover ?? '';
  230. $ptItem->ratio = $product->ratio ?? 1;
  231. $ptItem->shelfLife = $product->shelfLife ?? 7;
  232. $ptItem->weight = $product->weight ?? 0.1;
  233. $ptItem->stockWarning = $product->stockWarning ?? 5;;
  234. $ptItem->cost = $product->cost ?? 1;
  235. $ptItem->addPrice = 5;
  236. $ptItem->bigUnit = $product->bigUnit;
  237. $ptItem->smallUnit = $product->smallUnit;
  238. $ptItem->bigUnitId = $product->bigUnitId;
  239. $ptItem->smallUnitId = $product->smallUnitId;
  240. $ptItem->save();
  241. echo $product->id . " " . $product->name . " 已同步\n";
  242. }
  243. }
  244. //命令: ./yii item/sync name,cover 同步name 和cover
  245. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  246. public function actionSync($field)
  247. {
  248. $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning'];
  249. $fields = explode(",", $field);
  250. $syncFields = array_intersect($allFiled, $fields);
  251. if (empty($syncFields)) {
  252. echo "参数不支持";
  253. die;
  254. }
  255. $item = PtItemClass::getAllList("id", "");
  256. $itemIds = [];
  257. foreach ($item as $v) {
  258. $itemIds[] = $v['id'];
  259. }
  260. if (!empty($itemIds)) {
  261. foreach ($itemIds as $v) {
  262. $itemInfo = PtItemClass::getById($v);
  263. $data = [];
  264. foreach ($syncFields as $f) {
  265. $data[$f] = $itemInfo[$f];
  266. }
  267. ProductClass::updateByCondition(['itemId' => $v], $data);
  268. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data);
  269. }
  270. }
  271. echo "done";
  272. }
  273. //平台花材初始化导入 lqh 2021-05-08
  274. //命令: ./yii item/init-pt-item
  275. public function actionInitPtItem($clear = false)
  276. {
  277. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  278. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  279. $dir = Yii::$app->basePath;
  280. $filename = $dir . '/item.xls';
  281. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  282. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  283. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  284. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  285. $highestRow = $sheet->getHighestRow();//取得总行数
  286. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  287. $classData = [];
  288. $itemData = [];
  289. $classIdMap = [];
  290. for ($j = 2; $j <= $highestRow; $j++) {
  291. $str = '';
  292. for ($k = 'A'; $k <= $highestColumn; $k++) {
  293. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  294. }
  295. //explode:函数把字符串分割为数组。
  296. $string = explode("\\", $str);
  297. array_pop($string);
  298. $itemName = $string[0] ?? ''; //花材名称
  299. $className = $string[1] ?? ''; //分类名称
  300. $weight = $string[2] ?? 0; //重量 kg
  301. $bigUnit = $string[3] ?? ''; //大单位名称
  302. $smallUnit = $string[4] ?? ''; //小单位名称
  303. $ratio = $string[5] ?? 0; //比例
  304. $cost = $string[6] ?? 0;//成本价
  305. $addPrice = $string[7] ?? 0; //加价
  306. $stockWarning = $string[8] ?? 0;
  307. $alias = $string[9] ?? '';
  308. $cover = $string[10] ?? '';
  309. $inTurn = $string[11] ?? 0;
  310. if ($itemName) {
  311. $py = stringUtil::py($itemName);
  312. $item = [
  313. 'name' => $itemName,
  314. 'alias' => $alias,
  315. 'py' => $py,
  316. 'cover' => $cover,
  317. 'classId' => 0,
  318. 'ratio' => $ratio,
  319. 'weight' => $weight,
  320. 'stockWarning' => $stockWarning,
  321. 'cost' => $cost,
  322. 'addPrice' => $addPrice,
  323. 'bigUnit' => $bigUnit,
  324. 'smallUnit' => $smallUnit,
  325. 'inTurn' => $inTurn,
  326. ];
  327. $itemData[$className][] = $item;
  328. $classData[] = $className;
  329. }
  330. }
  331. $classData = array_unique($classData);
  332. //写入class
  333. if ($clear) {
  334. //清空数据
  335. Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute();
  336. Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute();
  337. }
  338. foreach ($classData as $v) {
  339. $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]);
  340. $classIdMap[$v] = $addClass['id'];
  341. }
  342. $insertItemData = [];
  343. foreach ($itemData as $k => $v) {
  344. $classId = $classIdMap[$k] ?? 0;
  345. if ($classId) {
  346. foreach ($v as $i) {
  347. $i['classId'] = $classId;
  348. $insertItemData[] = $i;
  349. }
  350. }
  351. }
  352. if (!empty($insertItemData)) {
  353. PtItemClass::batchAdd($insertItemData);
  354. }
  355. echo "done";
  356. }
  357. //供货商初始化 ./yii item/gys ./yii.bat item/gys
  358. public function actionGys()
  359. {
  360. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  361. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  362. $dir = Yii::$app->basePath;
  363. $filename = $dir . '/gys.xls';
  364. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  365. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  366. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  367. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  368. $highestRow = $sheet->getHighestRow();//取得总行数
  369. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  370. $changeData = [];
  371. for ($j = 2; $j <= $highestRow; $j++) {
  372. $str = '';
  373. for ($k = 'A'; $k <= $highestColumn; $k++) {
  374. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  375. }
  376. //explode:函数把字符串分割为数组。
  377. $string = explode("\\", $str);
  378. array_pop($string);
  379. array_push($changeData, $string);
  380. }
  381. if (!empty($changeData)) {
  382. $mobileList = array_values(array_column($changeData, 1));
  383. $ptStyle = dict::getDict('ptStyle', 'kmGhs');
  384. Yii::$app->params['ptStyle'] = $ptStyle;
  385. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  386. if (!empty($has)) {
  387. echo "这些已经添加过了,请先删除:\n";
  388. foreach ($has as $item) {
  389. echo $item->name . ' ' . $item->mobile . "\n";
  390. }
  391. exit();
  392. }
  393. if (getenv('YII_ENV') == 'production') {
  394. $this->sjId = 12367;
  395. $this->shopId = 10;
  396. } else {
  397. $this->sjId = 12615;
  398. $this->shopId = 36119;
  399. }
  400. foreach ($changeData as $currentKey => $currentData) {
  401. $mobile = $currentData[1] ?? '';
  402. $name = $currentData[0] ?? '';
  403. $address = $currentData[2] ?? '';
  404. if (stringUtil::isMobile($mobile) == false) {
  405. echo "请填写正确的手机号 \n";
  406. continue;
  407. }
  408. if (empty($name)) {
  409. echo "{$mobile} 名称不能为空 \n";
  410. continue;
  411. }
  412. if (stringUtil::getWordNum($name) > 8) {
  413. echo "{$mobile} 名称不能超过8个汉字 \n";
  414. continue;
  415. }
  416. $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  417. if (!empty($has)) {
  418. echo "{$mobile} 名称已经存在 \n";
  419. continue;
  420. }
  421. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  422. if (!empty($has)) {
  423. echo "{$mobile} 手机号已经添加过 \n";
  424. continue;
  425. }
  426. $connection = Yii::$app->db;
  427. $transaction = $connection->beginTransaction();
  428. try {
  429. $sjId = $this->sjId;
  430. $shopId = $this->shopId;
  431. $sjName = $this->sj->name ?? '';
  432. $applyData = [
  433. 'name' => $name,
  434. 'licenseNo' => $mobile,
  435. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  436. 'mobile' => $mobile,
  437. 'introSjId' => $sjId,
  438. 'introSjName' => $sjName,
  439. 'introShopId' => $shopId,
  440. 'introStyle' => SjClass::STYLE_SUPPLIER,
  441. 'from' => ApplyClass::FROM_GHS,
  442. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  443. 'address' => $address,
  444. 'status' => ApplyClass::STATUS_PASS,
  445. 'replace' => 1,//1表示供货商添加的客户或供货商
  446. ];
  447. $apply = ApplyService::replaceKmGhs($applyData);
  448. $id = $apply['id'] ?? 0;
  449. $respond = ApplyService::pass($id);
  450. $currentShop = $respond['shop'] ?? [];
  451. $sj = $respond['sj'] ?? [];
  452. $currentSjId = $sj['id'] ?? 0;
  453. $currentShopId = $currentShop->id ?? 0;
  454. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  455. if (empty($currentShopId)) {
  456. echo "{$mobile} 供货商添加失败 \n ";
  457. continue;
  458. }
  459. GhsClass::build($currentShopId, $shopId, 1);
  460. $transaction->commit();
  461. echo "{$mobile} 供货商添加成功!\n";
  462. } catch (\Exception $exception) {
  463. $transaction->rollBack();
  464. echo "{$mobile} 供货商添加失败,原因:" . $exception->getMessage() . "\n";
  465. }
  466. }
  467. }
  468. }
  469. //客户初始化 ./yii item/custom ./yii.bat item/custom
  470. public function actionCustom()
  471. {
  472. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  473. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  474. $dir = Yii::$app->basePath;
  475. $filename = $dir . '/custom.xls';
  476. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  477. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  478. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  479. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  480. $highestRow = $sheet->getHighestRow();//取得总行数
  481. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  482. $changeData = [];
  483. for ($j = 2; $j <= $highestRow; $j++) {
  484. $str = '';
  485. for ($k = 'A'; $k <= $highestColumn; $k++) {
  486. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  487. }
  488. //explode:函数把字符串分割为数组。
  489. $string = explode("\\", $str);
  490. array_pop($string);
  491. array_push($changeData, $string);
  492. }
  493. if (!empty($changeData)) {
  494. $mobileList = array_values(array_column($changeData, 1));
  495. $ptStyle = dict::getDict('ptStyle', 'hd');
  496. Yii::$app->params['ptStyle'] = $ptStyle;
  497. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  498. if (!empty($has)) {
  499. echo "这些已经添加过了,请先删除:\n";
  500. foreach ($has as $item) {
  501. echo $item->name . ' ' . $item->mobile . "\n";
  502. }
  503. exit();
  504. }
  505. if (getenv('YII_ENV') == 'production') {
  506. $this->sjId = 12367;
  507. $this->shopId = 10;
  508. } else {
  509. $this->sjId = 12615;
  510. $this->shopId = 36119;
  511. }
  512. $shop = ShopClass::getById($this->shopId, true);
  513. $sj = SjClass::getById($this->sjId, true);
  514. $this->sj = $sj;
  515. foreach ($changeData as $currentKey => $currentData) {
  516. $mobile = $currentData[1] ?? '';
  517. $name = $currentData[0] ?? '';
  518. $address = $currentData[2] ?? '';
  519. if (empty($name)) {
  520. echo "{$mobile} 名称不能为空 \n";
  521. continue;
  522. }
  523. if (stringUtil::getWordNum($name) > 8) {
  524. echo "{$mobile} 名称不能超过8个汉字 \n";
  525. continue;
  526. }
  527. if (stringUtil::isMobile($mobile) == false) {
  528. echo "{$mobile} 手机号错误 \n";
  529. continue;
  530. }
  531. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  532. if (!empty($has)) {
  533. echo "{$mobile} 名称已经存在 \n";
  534. continue;
  535. }
  536. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  537. if (!empty($has)) {
  538. echo "{$mobile} 手机号已经添加过 \n";
  539. continue;
  540. }
  541. $connection = Yii::$app->db;
  542. $transaction = $connection->beginTransaction();
  543. try {
  544. $sjId = $this->sjId;
  545. $shopId = $this->shopId;
  546. $sjName = $this->sj->name ?? '';
  547. $city = $shop->city ?? '';
  548. $dist = $shop->dist ?? '';
  549. $applyData = [
  550. 'name' => $name,
  551. 'licenseNo' => $mobile,
  552. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  553. 'mobile' => $mobile,
  554. 'introSjId' => $sjId,
  555. 'introSjName' => $sjName,
  556. 'introShopId' => $shopId,
  557. 'introStyle' => SjClass::STYLE_SUPPLIER,
  558. 'from' => ApplyClass::FROM_GHS,
  559. 'style' => SjClass::STYLE_RETAIL,
  560. 'province' => $shop->province ?? '',
  561. 'city' => $city,
  562. 'dist' => $dist,
  563. 'address' => $address,
  564. 'status' => ApplyClass::STATUS_PASS,
  565. 'replace' => 1,//1表示供货商添加的客户
  566. ];
  567. $apply = ApplyService::replaceRetail($applyData);
  568. $id = $apply['id'] ?? 0;
  569. $respond = ApplyService::pass($id);
  570. $currentShop = $respond['shop'] ?? [];
  571. $sj = $respond['sj'] ?? [];
  572. $hdSjId = $sj['id'] ?? 0;
  573. $hdShopId = $currentShop->id ?? 0;
  574. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  575. if (empty($hdShopId)) {
  576. echo "{$mobile} 客户添加失败 \n";
  577. continue;
  578. }
  579. $custom = CustomClass::build($this->shopId, $hdShopId);
  580. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  581. $transaction->commit();
  582. echo "{$mobile} 添加成功! \n";
  583. } catch (\Exception $exception) {
  584. $transaction->rollBack();
  585. echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
  586. }
  587. }
  588. }
  589. }
  590. }