ItemController.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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\product\classes\XjClass;
  7. use biz\sj\classes\SjClass;
  8. use bizGhs\custom\classes\CustomClass;
  9. use biz\shop\classes\ShopClass;
  10. use bizGhs\item\classes\ItemClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\order\classes\OrderClass;
  13. use bizGhs\order\classes\OrderItemClass;
  14. use bizGhs\order\classes\PurchaseOrderItemClass;
  15. use bizGhs\order\classes\StockWastageOrderClass;
  16. use bizGhs\order\classes\StockWastageOrderItemClass;
  17. use bizHd\saas\classes\ApplyClass;
  18. use bizHd\saas\models\Apply;
  19. use bizHd\saas\services\ApplyService;
  20. use common\components\dict;
  21. use common\components\imgUtil;
  22. use common\components\noticeUtil;
  23. use common\components\stringUtil;
  24. use common\components\util;
  25. use yii\console\Controller;
  26. use Yii;
  27. use bizGhs\product\classes\ProductClass;
  28. /**
  29. * 花材同步脚本 ssh 20230619
  30. */
  31. class ItemController extends Controller
  32. {
  33. public $sjId, $shopId, $sj;
  34. //复制迁移花材,请用这个方法,其他方法不用看了。迁移前从分类和花材清掉,然后下面相应的mainId修改一下就好了。 ssh 20260607
  35. public function actionMigrate()
  36. {
  37. if (getenv('YII_ENV') == 'production') {
  38. $newMainId = 100880;
  39. $oldMainId = 16299;
  40. } else {
  41. $newMainId = 762;
  42. $oldMainId = 644;
  43. util::stop('测试环境未开通');
  44. }
  45. $list = ItemClass::getAllByCondition(['mainId' => $newMainId], null, '*');
  46. if (!empty($list)) {
  47. echo '已经有花材了';
  48. exit();
  49. }
  50. $classList = ItemClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*', null, true);
  51. if (empty($classList)) {
  52. echo '没有找到老店的分类';
  53. exit();
  54. }
  55. $connection = Yii::$app->db;
  56. $transaction = $connection->beginTransaction();
  57. try {
  58. foreach ($classList as $class) {
  59. $oldId = $class->id ?? 0;
  60. $cover = $class->cover ?? '';
  61. $name = $class->name ?? '';
  62. $inTurn = $class->inTurn ?? 0;
  63. $isDefault = $class->isDefault ?? 0;
  64. $newClass = [
  65. 'mainId' => $newMainId,
  66. 'cover' => $cover,
  67. 'name' => $name,
  68. 'inTurn' => $inTurn,
  69. 'isDefault' => $isDefault,
  70. ];
  71. $newRespond = ItemClassClass::add($newClass, true);
  72. $newClassId = $newRespond->id ?? 0;
  73. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  74. foreach ($oldItemList as $oldItem) {
  75. $oldItemId = $oldItem['id'];
  76. unset($oldItem['id']);
  77. $oldItem['classId'] = $newClassId;
  78. $oldItem['stock'] = 0;
  79. $oldItem['onStock'] = 0;
  80. $oldItem['status'] = 2;
  81. $oldItem['mainId'] = $newMainId;
  82. $oldItem['sjId'] = 0;
  83. $oldItem['shopId'] = 0;
  84. $new = ItemClass::add($oldItem, true);
  85. if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
  86. $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
  87. if (!empty($xjList)) {
  88. foreach ($xjList as $oneXj) {
  89. unset($oneXj['id']);
  90. unset($oneXj['sjId']);
  91. unset($oneXj['shopId']);
  92. unset($oneXj['mainId']);
  93. unset($oneXj['itemId']);
  94. unset($oneXj['ptItemId']);
  95. unset($oneXj['addTime']);
  96. unset($oneXj['updateTime']);
  97. $oneXj['mainId'] = $newMainId;
  98. $oneXj['itemId'] = $new->id ?? 0;
  99. $oneXj['ptItemId'] = $new->itemId ?? 0;
  100. XjClass::add($oneXj);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. $transaction->commit();
  107. } catch (\Exception $e) {
  108. $transaction->rollBack();
  109. $msg = $e->getMessage();
  110. echo "报错了:" . $msg;
  111. }
  112. }
  113. public function actionCQ()
  114. {
  115. $itemList = ItemClass::getAllByCondition(['mainId' => 14128], null, '*', null, true);
  116. if (!empty($itemList)) {
  117. foreach ($itemList as $item) {
  118. $classId = $item->classId;
  119. if (!empty($classId)) {
  120. $class = ItemClassClass::getById($classId, true);
  121. if ($class->mainId != 14128) {
  122. echo $item->name . "\n";
  123. }
  124. }
  125. }
  126. }
  127. }
  128. //花材修改分类同步 ssh 20240523
  129. public function actionModifyItemClassSync()
  130. {
  131. ini_set('memory_limit', '5045M');
  132. set_time_limit(0);
  133. $productIdCachedKey = 'item_class_modify_sync';
  134. $idList = [];
  135. while ($count = Yii::$app->redis->executeCommand('LLEN', [$productIdCachedKey])) {
  136. if (count($idList) > 20) {
  137. break;
  138. }
  139. $currentId = Yii::$app->redis->executeCommand('RPOP', [$productIdCachedKey]);
  140. $idList[] = $currentId;
  141. }
  142. if (empty($idList)) {
  143. return false;
  144. }
  145. $list = ProductClass::getAllByCondition(['id' => ['in', $idList]], null, '*', null, true);
  146. if (!empty($list)) {
  147. foreach ($list as $info) {
  148. $classId = $info->classId ?? 0;
  149. $productId = $info->id ?? 0;
  150. OrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  151. \bizHd\order\classes\OrderItemClass::updateByCondition(['itemId' => $productId], ['classId' => $classId]);
  152. PurchaseOrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  153. //noticeUtil::push("productId:{$productId} classId:{$classId}", '15280215347');
  154. }
  155. }
  156. //noticeUtil::push(implode(',', $idList) . ' 这些花材ID修改了分类,已同步到订单和采购单', '15280215347');
  157. }
  158. //【重要】将花材的分类id同步到批发销售单、批发采购单和零售销售单【小丽优先】
  159. public function actionSyncClass()
  160. {
  161. ini_set('memory_limit', '5120M');
  162. set_time_limit(0);
  163. $itemList = ItemClass::getAllByCondition(['mainId' => 10975], null, '*', null, true);
  164. if (empty($itemList)) {
  165. util::stop('没有找到花材');
  166. }
  167. foreach ($itemList as $item) {
  168. $classId = $item->classId ?? 0;
  169. $productId = $item->id ?? 0;
  170. if (!empty($classId) && !empty($productId)) {
  171. OrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  172. \bizHd\order\classes\OrderItemClass::updateByCondition(['itemId' => $productId], ['classId' => $classId]);
  173. PurchaseOrderItemClass::updateByCondition(['productId' => $productId], ['classId' => $classId]);
  174. }
  175. }
  176. echo "ok\n";
  177. }
  178. //包装纸丝带同步 ssh 20230525
  179. public function actionPaperSync()
  180. {
  181. $connection = Yii::$app->db;
  182. $transaction = $connection->beginTransaction();
  183. try {
  184. $oldItemList = ItemClass::getAllByCondition(['classId' => 26013, 'delStatus' => 0], null, '*');
  185. foreach ($oldItemList as $oldItem) {
  186. unset($oldItem['id']);
  187. $oldItem['classId'] = 31106;
  188. $oldItem['stock'] = 0;
  189. $oldItem['status'] = 2;
  190. $oldItem['mainId'] = 4553;
  191. $oldItem['sjId'] = 16936;
  192. $oldItem['shopId'] = 0;
  193. ItemClass::add($oldItem);
  194. }
  195. $transaction->commit();
  196. } catch (\Exception $e) {
  197. $transaction->rollBack();
  198. $msg = $e->getMessage();
  199. echo "报错了:" . $msg;
  200. }
  201. }
  202. public function actionWaste()
  203. {
  204. ini_set('memory_limit', '2045M');
  205. set_time_limit(0);
  206. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  207. if (!empty($list)) {
  208. foreach ($list as $order) {
  209. $orderSn = $order->orderSn ?? '';
  210. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  211. $totalCost = 0;
  212. if (!empty($itemList)) {
  213. foreach ($itemList as $v) {
  214. $num = $v->itemNum ?? 0;
  215. $itemCost = $v->itemCost ?? 0;
  216. $totalCost = bcadd($totalCost, bcmul($num, $itemCost, 2), 2);
  217. }
  218. }
  219. $order->cost = $totalCost;
  220. $order->save();
  221. }
  222. }
  223. }
  224. public function actionWastage()
  225. {
  226. ini_set('memory_limit', '2045M');
  227. set_time_limit(0);
  228. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  229. if (!empty($list)) {
  230. foreach ($list as $order) {
  231. $orderSn = $order->orderSn ?? '';
  232. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  233. $totalPrice = 0;
  234. if (!empty($itemList)) {
  235. foreach ($itemList as $v) {
  236. $num = $v->itemNum ?? 0;
  237. $itemPrice = $v->itemPrice ?? 0;
  238. $totalPrice = bcadd($totalPrice, bcmul($num, $itemPrice, 2), 2);
  239. }
  240. }
  241. $order->price = $totalPrice;
  242. $order->save();
  243. }
  244. }
  245. }
  246. //小向花材迁移 ssh 20250215
  247. public function actionXx()
  248. {
  249. if (getenv('YII_ENV') == 'production') {
  250. $newMainId = 49988;
  251. $oldMainId = 23390;
  252. } else {
  253. $newMainId = 762;
  254. $oldMainId = 644;
  255. util::stop('已取消');
  256. }
  257. $list = ItemClass::getAllByCondition(['mainId' => $newMainId], null, '*');
  258. if (!empty($list)) {
  259. echo '已经有花材了';
  260. exit();
  261. }
  262. $classList = ItemClassClass::getAllByCondition(['mainId' => $oldMainId], null, '*', null, true);
  263. if (empty($classList)) {
  264. echo '没有找到目标门店的分类';
  265. exit();
  266. }
  267. $connection = Yii::$app->db;
  268. $transaction = $connection->beginTransaction();
  269. try {
  270. foreach ($classList as $class) {
  271. $oldId = $class->id ?? 0;
  272. $cover = $class->cover ?? '';
  273. $name = $class->name ?? '';
  274. $inTurn = $class->inTurn ?? 0;
  275. $isDefault = $class->isDefault ?? 0;
  276. $newClass = [
  277. 'mainId' => $newMainId,
  278. 'cover' => $cover,
  279. 'name' => $name,
  280. 'inTurn' => $inTurn,
  281. 'isDefault' => $isDefault,
  282. ];
  283. $newRespond = ItemClassClass::add($newClass, true);
  284. $newClassId = $newRespond->id ?? 0;
  285. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  286. foreach ($oldItemList as $oldItem) {
  287. $oldItemId = $oldItem['id'];
  288. unset($oldItem['id']);
  289. $oldItem['classId'] = $newClassId;
  290. $oldItem['stock'] = 0;
  291. $oldItem['status'] = 2;
  292. $oldItem['mainId'] = $newMainId;
  293. $oldItem['sjId'] = 0;
  294. $oldItem['shopId'] = 0;
  295. $new = ItemClass::add($oldItem, true);
  296. if (isset($oldItem['variety']) && $oldItem['variety'] == 1) {
  297. $xjList = XjClass::getAllByCondition(['itemId' => $oldItemId], null, '*');
  298. if (!empty($xjList)) {
  299. foreach ($xjList as $oneXj) {
  300. unset($oneXj['id']);
  301. unset($oneXj['sjId']);
  302. unset($oneXj['shopId']);
  303. unset($oneXj['mainId']);
  304. unset($oneXj['itemId']);
  305. unset($oneXj['ptItemId']);
  306. unset($oneXj['addTime']);
  307. unset($oneXj['updateTime']);
  308. $oneXj['mainId'] = $newMainId;
  309. $oneXj['itemId'] = $new->id ?? 0;
  310. $oneXj['ptItemId'] = $new->itemId ?? 0;
  311. XjClass::add($oneXj);
  312. }
  313. }
  314. }
  315. }
  316. }
  317. $transaction->commit();
  318. } catch (\Exception $e) {
  319. $transaction->rollBack();
  320. $msg = $e->getMessage();
  321. echo "报错了:" . $msg;
  322. }
  323. }
  324. //花材反向更新 ./yii item/reverse-update
  325. public function actionReverseUpdate()
  326. {
  327. $shopId = 10;
  328. if (getenv('YII_ENV') != 'production') {
  329. $shopId = 36225;
  330. }
  331. $list = ProductClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
  332. if (empty($list)) {
  333. return false;
  334. }
  335. foreach ($list as $cKey => $product) {
  336. $ptItemId = $product->itemId ?? 0;
  337. $ptItem = PtItemClass::getById($ptItemId, true);
  338. if (empty($ptItem)) {
  339. continue;
  340. }
  341. $ptItem->name = $product->name ?? '';
  342. $ptItem->py = $product->py ?? '';
  343. $ptItem->cover = $product->cover ?? '';
  344. $ptItem->ratio = $product->ratio ?? 1;
  345. $ptItem->shelfLife = $product->shelfLife ?? 7;
  346. $ptItem->weight = $product->weight ?? 0.1;
  347. $ptItem->stockWarning = $product->stockWarning ?? 5;;
  348. $ptItem->cost = $product->cost ?? 1;
  349. $ptItem->addPrice = 5;
  350. $ptItem->bigUnit = $product->bigUnit;
  351. $ptItem->smallUnit = $product->smallUnit;
  352. $ptItem->bigUnitId = $product->bigUnitId;
  353. $ptItem->smallUnitId = $product->smallUnitId;
  354. $ptItem->save();
  355. echo $product->id . " " . $product->name . " 已同步\n";
  356. }
  357. }
  358. //命令: ./yii item/sync name,cover 同步name 和cover
  359. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  360. public function actionSync($field)
  361. {
  362. $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning'];
  363. $fields = explode(",", $field);
  364. $syncFields = array_intersect($allFiled, $fields);
  365. if (empty($syncFields)) {
  366. echo "参数不支持";
  367. die;
  368. }
  369. $item = PtItemClass::getAllList("id", "");
  370. $itemIds = [];
  371. foreach ($item as $v) {
  372. $itemIds[] = $v['id'];
  373. }
  374. if (!empty($itemIds)) {
  375. foreach ($itemIds as $v) {
  376. $itemInfo = PtItemClass::getById($v);
  377. $data = [];
  378. foreach ($syncFields as $f) {
  379. $data[$f] = $itemInfo[$f];
  380. }
  381. ProductClass::updateByCondition(['itemId' => $v], $data);
  382. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data);
  383. }
  384. }
  385. echo "done";
  386. }
  387. //平台花材初始化导入 lqh 2021-05-08
  388. //命令: ./yii item/init-pt-item
  389. public function actionInitPtItem($clear = false)
  390. {
  391. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  392. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  393. $dir = Yii::$app->basePath;
  394. $filename = $dir . '/item.xls';
  395. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  396. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  397. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  398. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  399. $highestRow = $sheet->getHighestRow();//取得总行数
  400. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  401. $classData = [];
  402. $itemData = [];
  403. $classIdMap = [];
  404. for ($j = 2; $j <= $highestRow; $j++) {
  405. $str = '';
  406. for ($k = 'A'; $k <= $highestColumn; $k++) {
  407. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  408. }
  409. //explode:函数把字符串分割为数组。
  410. $string = explode("\\", $str);
  411. array_pop($string);
  412. $itemName = $string[0] ?? ''; //花材名称
  413. $className = $string[1] ?? ''; //分类名称
  414. $weight = $string[2] ?? 0; //重量 kg
  415. $bigUnit = $string[3] ?? ''; //大单位名称
  416. $smallUnit = $string[4] ?? ''; //小单位名称
  417. $ratio = $string[5] ?? 0; //比例
  418. $cost = $string[6] ?? 0;//成本价
  419. $addPrice = $string[7] ?? 0; //加价
  420. $stockWarning = $string[8] ?? 0;
  421. $alias = $string[9] ?? '';
  422. $cover = $string[10] ?? '';
  423. $inTurn = $string[11] ?? 0;
  424. if ($itemName) {
  425. $py = stringUtil::py($itemName);
  426. $item = [
  427. 'name' => $itemName,
  428. 'alias' => $alias,
  429. 'py' => $py,
  430. 'cover' => $cover,
  431. 'classId' => 0,
  432. 'ratio' => $ratio,
  433. 'weight' => $weight,
  434. 'stockWarning' => $stockWarning,
  435. 'cost' => $cost,
  436. 'addPrice' => $addPrice,
  437. 'bigUnit' => $bigUnit,
  438. 'smallUnit' => $smallUnit,
  439. 'inTurn' => $inTurn,
  440. ];
  441. $itemData[$className][] = $item;
  442. $classData[] = $className;
  443. }
  444. }
  445. $classData = array_unique($classData);
  446. //写入class
  447. if ($clear) {
  448. //清空数据
  449. Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute();
  450. Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute();
  451. }
  452. foreach ($classData as $v) {
  453. $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]);
  454. $classIdMap[$v] = $addClass['id'];
  455. }
  456. $insertItemData = [];
  457. foreach ($itemData as $k => $v) {
  458. $classId = $classIdMap[$k] ?? 0;
  459. if ($classId) {
  460. foreach ($v as $i) {
  461. $i['classId'] = $classId;
  462. $insertItemData[] = $i;
  463. }
  464. }
  465. }
  466. if (!empty($insertItemData)) {
  467. PtItemClass::batchAdd($insertItemData);
  468. }
  469. echo "done";
  470. }
  471. //供货商初始化 ./yii item/gys ./yii.bat item/gys
  472. public function actionGys()
  473. {
  474. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  475. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  476. $dir = Yii::$app->basePath;
  477. $filename = $dir . '/gys.xls';
  478. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  479. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  480. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  481. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  482. $highestRow = $sheet->getHighestRow();//取得总行数
  483. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  484. $changeData = [];
  485. for ($j = 2; $j <= $highestRow; $j++) {
  486. $str = '';
  487. for ($k = 'A'; $k <= $highestColumn; $k++) {
  488. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  489. }
  490. //explode:函数把字符串分割为数组。
  491. $string = explode("\\", $str);
  492. array_pop($string);
  493. array_push($changeData, $string);
  494. }
  495. if (!empty($changeData)) {
  496. $mobileList = array_values(array_column($changeData, 1));
  497. $ptStyle = dict::getDict('ptStyle', 'kmGhs');
  498. Yii::$app->params['ptStyle'] = $ptStyle;
  499. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  500. if (!empty($has)) {
  501. echo "这些已经添加过了,请先删除:\n";
  502. foreach ($has as $item) {
  503. echo $item->name . ' ' . $item->mobile . "\n";
  504. }
  505. exit();
  506. }
  507. if (getenv('YII_ENV') == 'production') {
  508. $this->sjId = 12367;
  509. $this->shopId = 10;
  510. } else {
  511. $this->sjId = 12615;
  512. $this->shopId = 36119;
  513. }
  514. foreach ($changeData as $currentKey => $currentData) {
  515. $mobile = $currentData[1] ?? '';
  516. $name = $currentData[0] ?? '';
  517. $address = $currentData[2] ?? '';
  518. if (stringUtil::isMobile($mobile) == false) {
  519. echo "请填写正确的手机号 \n";
  520. continue;
  521. }
  522. if (empty($name)) {
  523. echo "{$mobile} 名称不能为空 \n";
  524. continue;
  525. }
  526. if (stringUtil::getWordNum($name) > 8) {
  527. echo "{$mobile} 名称不能超过8个汉字 \n";
  528. continue;
  529. }
  530. $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  531. if (!empty($has)) {
  532. echo "{$mobile} 名称已经存在 \n";
  533. continue;
  534. }
  535. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  536. if (!empty($has)) {
  537. echo "{$mobile} 手机号已经添加过 \n";
  538. continue;
  539. }
  540. $connection = Yii::$app->db;
  541. $transaction = $connection->beginTransaction();
  542. try {
  543. $sjId = $this->sjId;
  544. $shopId = $this->shopId;
  545. $sjName = $this->sj->name ?? '';
  546. $applyData = [
  547. 'name' => $name,
  548. 'licenseNo' => $mobile,
  549. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  550. 'mobile' => $mobile,
  551. 'introSjId' => $sjId,
  552. 'introSjName' => $sjName,
  553. 'introShopId' => $shopId,
  554. 'introStyle' => SjClass::STYLE_SUPPLIER,
  555. 'from' => ApplyClass::FROM_GHS,
  556. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  557. 'address' => $address,
  558. 'status' => ApplyClass::STATUS_PASS,
  559. 'replace' => 1,//1表示供货商添加的客户或供货商
  560. ];
  561. $apply = ApplyService::replaceKmGhs($applyData);
  562. $id = $apply['id'] ?? 0;
  563. $respond = ApplyService::pass($id);
  564. $currentShop = $respond['shop'] ?? [];
  565. $sj = $respond['sj'] ?? [];
  566. $currentSjId = $sj['id'] ?? 0;
  567. $currentShopId = $currentShop->id ?? 0;
  568. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  569. if (empty($currentShopId)) {
  570. echo "{$mobile} 供货商添加失败 \n ";
  571. continue;
  572. }
  573. GhsClass::build($currentShopId, $shopId, 1);
  574. $transaction->commit();
  575. echo "{$mobile} 供货商添加成功!\n";
  576. } catch (\Exception $exception) {
  577. $transaction->rollBack();
  578. echo "{$mobile} 供货商添加失败,原因:" . $exception->getMessage() . "\n";
  579. }
  580. }
  581. }
  582. }
  583. //客户初始化 ./yii item/custom ./yii.bat item/custom
  584. public function actionCustom()
  585. {
  586. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  587. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  588. $dir = Yii::$app->basePath;
  589. $filename = $dir . '/custom.xls';
  590. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  591. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  592. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  593. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  594. $highestRow = $sheet->getHighestRow();//取得总行数
  595. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  596. $changeData = [];
  597. for ($j = 2; $j <= $highestRow; $j++) {
  598. $str = '';
  599. for ($k = 'A'; $k <= $highestColumn; $k++) {
  600. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  601. }
  602. //explode:函数把字符串分割为数组。
  603. $string = explode("\\", $str);
  604. array_pop($string);
  605. array_push($changeData, $string);
  606. }
  607. if (!empty($changeData)) {
  608. $mobileList = array_values(array_column($changeData, 1));
  609. $ptStyle = dict::getDict('ptStyle', 'hd');
  610. Yii::$app->params['ptStyle'] = $ptStyle;
  611. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  612. if (!empty($has)) {
  613. echo "这些已经添加过了,请先删除:\n";
  614. foreach ($has as $item) {
  615. echo $item->name . ' ' . $item->mobile . "\n";
  616. }
  617. exit();
  618. }
  619. if (getenv('YII_ENV') == 'production') {
  620. $this->sjId = 12367;
  621. $this->shopId = 10;
  622. } else {
  623. $this->sjId = 12615;
  624. $this->shopId = 36119;
  625. }
  626. $shop = ShopClass::getById($this->shopId, true);
  627. $sj = SjClass::getById($this->sjId, true);
  628. $this->sj = $sj;
  629. foreach ($changeData as $currentKey => $currentData) {
  630. $mobile = $currentData[1] ?? '';
  631. $name = $currentData[0] ?? '';
  632. $address = $currentData[2] ?? '';
  633. if (empty($name)) {
  634. echo "{$mobile} 名称不能为空 \n";
  635. continue;
  636. }
  637. if (stringUtil::getWordNum($name) > 8) {
  638. echo "{$mobile} 名称不能超过8个汉字 \n";
  639. continue;
  640. }
  641. if (stringUtil::isMobile($mobile) == false) {
  642. echo "{$mobile} 手机号错误 \n";
  643. continue;
  644. }
  645. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  646. if (!empty($has)) {
  647. echo "{$mobile} 名称已经存在 \n";
  648. continue;
  649. }
  650. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  651. if (!empty($has)) {
  652. echo "{$mobile} 手机号已经添加过 \n";
  653. continue;
  654. }
  655. $connection = Yii::$app->db;
  656. $transaction = $connection->beginTransaction();
  657. try {
  658. $sjId = $this->sjId;
  659. $shopId = $this->shopId;
  660. $sjName = $this->sj->name ?? '';
  661. $city = $shop->city ?? '';
  662. $dist = $shop->dist ?? '';
  663. $applyData = [
  664. 'name' => $name,
  665. 'licenseNo' => $mobile,
  666. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  667. 'mobile' => $mobile,
  668. 'introSjId' => $sjId,
  669. 'introSjName' => $sjName,
  670. 'introShopId' => $shopId,
  671. 'introStyle' => SjClass::STYLE_SUPPLIER,
  672. 'from' => ApplyClass::FROM_GHS,
  673. 'style' => SjClass::STYLE_RETAIL,
  674. 'province' => $shop->province ?? '',
  675. 'city' => $city,
  676. 'dist' => $dist,
  677. 'address' => $address,
  678. 'status' => ApplyClass::STATUS_PASS,
  679. 'replace' => 1,//1表示供货商添加的客户
  680. ];
  681. $apply = ApplyService::replaceRetail($applyData);
  682. $id = $apply['id'] ?? 0;
  683. $respond = ApplyService::pass($id);
  684. $currentShop = $respond['shop'] ?? [];
  685. $sj = $respond['sj'] ?? [];
  686. $hdSjId = $sj['id'] ?? 0;
  687. $hdShopId = $currentShop->id ?? 0;
  688. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  689. if (empty($hdShopId)) {
  690. echo "{$mobile} 客户添加失败 \n";
  691. continue;
  692. }
  693. $custom = CustomClass::build($this->shopId, $hdShopId);
  694. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  695. $transaction->commit();
  696. echo "{$mobile} 添加成功! \n";
  697. } catch (\Exception $exception) {
  698. $transaction->rollBack();
  699. echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
  700. }
  701. }
  702. }
  703. }
  704. //更新特价 ssh 20230630
  705. public function actionDiscount()
  706. {
  707. ini_set('memory_limit', '2045M');
  708. set_time_limit(0);
  709. $list = ItemClass::getAllByCondition(['discountPrice>' => 0,], null, '*', null, true);
  710. if (!empty($list)) {
  711. foreach ($list as $item) {
  712. $discountPrice = $item->discountPrice ?? 0;
  713. $skMore = $item->skMore ?? 0;
  714. $addPrice = $item->addPrice ?? 0;
  715. $diff = bcsub($skMore, $addPrice, 2);
  716. if ($diff > 0) {
  717. $item->skDiscountPrice = bcadd($discountPrice, $diff, 2);
  718. } else {
  719. $item->skDiscountPrice = $discountPrice;
  720. }
  721. $item->hjDiscountPrice = $discountPrice;
  722. $item->save();
  723. }
  724. }
  725. }
  726. }