ItemController.php 28 KB

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