ItemController.php 28 KB

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