ItemController.php 31 KB

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