ItemController.php 28 KB

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