ItemController.php 31 KB

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