ItemController.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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\sj\classes\SjClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use biz\shop\classes\ShopClass;
  9. use bizGhs\item\classes\ItemClass;
  10. use bizGhs\item\classes\ItemClassClass;
  11. use bizGhs\order\classes\StockWastageOrderClass;
  12. use bizGhs\order\classes\StockWastageOrderItemClass;
  13. use bizHd\saas\classes\ApplyClass;
  14. use bizHd\saas\models\Apply;
  15. use bizHd\saas\services\ApplyService;
  16. use common\components\dict;
  17. use common\components\imgUtil;
  18. use common\components\stringUtil;
  19. use yii\console\Controller;
  20. use Yii;
  21. use bizGhs\product\classes\ProductClass;
  22. /**
  23. * 花材初始化 ssh 2021.2.18
  24. */
  25. class ItemController extends Controller
  26. {
  27. public $sjId, $shopId, $sj;
  28. public function actionWastage()
  29. {
  30. ini_set('memory_limit', '2045M');
  31. set_time_limit(0);
  32. $list = StockWastageOrderClass::getAllByCondition(['id>' => 0], null, '*', null, true);
  33. if (!empty($list)) {
  34. foreach ($list as $order) {
  35. $orderSn = $order->orderSn ?? '';
  36. $itemList = StockWastageOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  37. $totalPrice = 0;
  38. if (!empty($itemList)) {
  39. foreach ($itemList as $v) {
  40. $num = $v->itemNum ?? 0;
  41. $itemPrice = $v->itemPrice ?? 0;
  42. $totalPrice = bcadd($totalPrice, bcmul($num, $itemPrice, 2), 2);
  43. }
  44. }
  45. $order->price = $totalPrice;
  46. $order->save();
  47. }
  48. }
  49. }
  50. public function actionMt()
  51. {
  52. if (getenv('YII_ENV') == 'production') {
  53. $mtMainId = 3923;
  54. $ttMainId = 742;
  55. } else {
  56. $mtMainId = 703;
  57. $ttMainId = 644;
  58. }
  59. $list = ItemClass::getAllByCondition(['mainId' => $mtMainId], null, '*');
  60. if (!empty($list)) {
  61. echo '已经有花材了';
  62. exit();
  63. }
  64. $classList = ItemClassClass::getAllByCondition(['mainId' => $ttMainId], null, '*', null, true);
  65. if (empty($classList)) {
  66. echo '没有找到天天鲜花的分类';
  67. exit();
  68. }
  69. $connection = Yii::$app->db;
  70. $transaction = $connection->beginTransaction();
  71. try {
  72. foreach ($classList as $class) {
  73. $oldId = $class->id ?? 0;
  74. $cover = $class->cover ?? '';
  75. $name = $class->name ?? '';
  76. $inTurn = $class->inTurn ?? 0;
  77. $isDefault = $class->isDefault ?? 0;
  78. $newClass = [
  79. 'mainId' => $mtMainId,
  80. 'cover' => $cover,
  81. 'name' => $name,
  82. 'inTurn' => $inTurn,
  83. 'isDefault' => $isDefault,
  84. ];
  85. $newRespond = ItemClassClass::add($newClass, true);
  86. $newClassId = $newRespond->id ?? 0;
  87. $oldItemList = ItemClass::getAllByCondition(['classId' => $oldId, 'delStatus' => 0], null, '*');
  88. foreach ($oldItemList as $oldItem) {
  89. unset($oldItem['id']);
  90. $oldItem['classId'] = $newClassId;
  91. $oldItem['stock'] = 0;
  92. $oldItem['status'] = 2;
  93. $oldItem['mainId'] = $mtMainId;
  94. $oldItem['sjId'] = 16300;
  95. $oldItem['shopId'] = 0;
  96. ItemClass::add($oldItem);
  97. }
  98. }
  99. $transaction->commit();
  100. } catch (\Exception $e) {
  101. $transaction->rollBack();
  102. $msg = $e->getMessage();
  103. echo "报错了:" . $msg;
  104. }
  105. }
  106. //花材反向更新 ./yii item/reverse-update
  107. public function actionReverseUpdate()
  108. {
  109. $shopId = 10;
  110. if (getenv('YII_ENV') != 'production') {
  111. $shopId = 36225;
  112. }
  113. $list = ProductClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
  114. if (empty($list)) {
  115. return false;
  116. }
  117. foreach ($list as $cKey => $product) {
  118. $ptItemId = $product->itemId ?? 0;
  119. $ptItem = PtItemClass::getById($ptItemId, true);
  120. if (empty($ptItem)) {
  121. continue;
  122. }
  123. $ptItem->name = $product->name ?? '';
  124. $ptItem->py = $product->py ?? '';
  125. $ptItem->cover = $product->cover ?? '';
  126. $ptItem->ratio = $product->ratio ?? 1;
  127. $ptItem->shelfLife = $product->shelfLife ?? 7;
  128. $ptItem->weight = $product->weight ?? 0.1;
  129. $ptItem->stockWarning = $product->stockWarning ?? 5;;
  130. $ptItem->cost = $product->cost ?? 1;
  131. $ptItem->addPrice = 5;
  132. $ptItem->bigUnit = $product->bigUnit;
  133. $ptItem->smallUnit = $product->smallUnit;
  134. $ptItem->bigUnitId = $product->bigUnitId;
  135. $ptItem->smallUnitId = $product->smallUnitId;
  136. $ptItem->save();
  137. echo $product->id . " " . $product->name . " 已同步\n";
  138. }
  139. }
  140. //命令: ./yii item/sync name,cover 同步name 和cover
  141. //同步item, ptItem更新,同步到 ghsItem, ghsProduct, 同步字段(name,alias,py,cover,ratio,cover等)
  142. public function actionSync($field)
  143. {
  144. $allFiled = ['name', 'alias', 'cover', 'py', 'ratio', 'weight', 'bigUnit', 'smallUnit', 'bigUnitId', 'smallUnitId', 'stockWarning'];
  145. $fields = explode(",", $field);
  146. $syncFields = array_intersect($allFiled, $fields);
  147. if (empty($syncFields)) {
  148. echo "参数不支持";
  149. die;
  150. }
  151. $item = PtItemClass::getAllList("id", "");
  152. $itemIds = [];
  153. foreach ($item as $v) {
  154. $itemIds[] = $v['id'];
  155. }
  156. if (!empty($itemIds)) {
  157. foreach ($itemIds as $v) {
  158. $itemInfo = PtItemClass::getById($v);
  159. $data = [];
  160. foreach ($syncFields as $f) {
  161. $data[$f] = $itemInfo[$f];
  162. }
  163. ProductClass::updateByCondition(['itemId' => $v], $data);
  164. \bizGhs\item\classes\ItemClass::updateByCondition(['itemId' => $v], $data);
  165. }
  166. }
  167. echo "done";
  168. }
  169. //平台花材初始化导入 lqh 2021-05-08
  170. //命令: ./yii item/init-pt-item
  171. public function actionInitPtItem($clear = false)
  172. {
  173. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  174. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  175. $dir = Yii::$app->basePath;
  176. $filename = $dir . '/item.xls';
  177. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  178. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  179. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  180. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  181. $highestRow = $sheet->getHighestRow();//取得总行数
  182. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  183. $classData = [];
  184. $itemData = [];
  185. $classIdMap = [];
  186. for ($j = 2; $j <= $highestRow; $j++) {
  187. $str = '';
  188. for ($k = 'A'; $k <= $highestColumn; $k++) {
  189. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  190. }
  191. //explode:函数把字符串分割为数组。
  192. $string = explode("\\", $str);
  193. array_pop($string);
  194. $itemName = $string[0] ?? ''; //花材名称
  195. $className = $string[1] ?? ''; //分类名称
  196. $weight = $string[2] ?? 0; //重量 kg
  197. $bigUnit = $string[3] ?? ''; //大单位名称
  198. $smallUnit = $string[4] ?? ''; //小单位名称
  199. $ratio = $string[5] ?? 0; //比例
  200. $cost = $string[6] ?? 0;//成本价
  201. $addPrice = $string[7] ?? 0; //加价
  202. $stockWarning = $string[8] ?? 0;
  203. $alias = $string[9] ?? '';
  204. $cover = $string[10] ?? '';
  205. $inTurn = $string[11] ?? 0;
  206. if ($itemName) {
  207. $py = stringUtil::py($itemName);
  208. $item = [
  209. 'name' => $itemName,
  210. 'alias' => $alias,
  211. 'py' => $py,
  212. 'cover' => $cover,
  213. 'classId' => 0,
  214. 'ratio' => $ratio,
  215. 'weight' => $weight,
  216. 'stockWarning' => $stockWarning,
  217. 'cost' => $cost,
  218. 'addPrice' => $addPrice,
  219. 'bigUnit' => $bigUnit,
  220. 'smallUnit' => $smallUnit,
  221. 'inTurn' => $inTurn,
  222. ];
  223. $itemData[$className][] = $item;
  224. $classData[] = $className;
  225. }
  226. }
  227. $classData = array_unique($classData);
  228. //写入class
  229. if ($clear) {
  230. //清空数据
  231. Yii::$app->db->createCommand()->truncateTable('xhPtItemClass')->execute();
  232. Yii::$app->db->createCommand()->truncateTable('xhPtItem')->execute();
  233. }
  234. foreach ($classData as $v) {
  235. $addClass = PtItemClassClass::add(['name' => $v, 'inTurn' => 0, 'group' => 0]);
  236. $classIdMap[$v] = $addClass['id'];
  237. }
  238. $insertItemData = [];
  239. foreach ($itemData as $k => $v) {
  240. $classId = $classIdMap[$k] ?? 0;
  241. if ($classId) {
  242. foreach ($v as $i) {
  243. $i['classId'] = $classId;
  244. $insertItemData[] = $i;
  245. }
  246. }
  247. }
  248. if (!empty($insertItemData)) {
  249. PtItemClass::batchAdd($insertItemData);
  250. }
  251. echo "done";
  252. }
  253. //供货商初始化 ./yii item/gys ./yii.bat item/gys
  254. public function actionGys()
  255. {
  256. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  257. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  258. $dir = Yii::$app->basePath;
  259. $filename = $dir . '/gys.xls';
  260. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  261. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  262. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  263. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  264. $highestRow = $sheet->getHighestRow();//取得总行数
  265. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  266. $changeData = [];
  267. for ($j = 2; $j <= $highestRow; $j++) {
  268. $str = '';
  269. for ($k = 'A'; $k <= $highestColumn; $k++) {
  270. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  271. }
  272. //explode:函数把字符串分割为数组。
  273. $string = explode("\\", $str);
  274. array_pop($string);
  275. array_push($changeData, $string);
  276. }
  277. if (!empty($changeData)) {
  278. $mobileList = array_values(array_column($changeData, 1));
  279. $ptStyle = dict::getDict('ptStyle', 'kmGhs');
  280. Yii::$app->params['ptStyle'] = $ptStyle;
  281. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  282. if (!empty($has)) {
  283. echo "这些已经添加过了,请先删除:\n";
  284. foreach ($has as $item) {
  285. echo $item->name . ' ' . $item->mobile . "\n";
  286. }
  287. exit();
  288. }
  289. if (getenv('YII_ENV') == 'production') {
  290. $this->sjId = 12367;
  291. $this->shopId = 10;
  292. } else {
  293. $this->sjId = 12615;
  294. $this->shopId = 36119;
  295. }
  296. foreach ($changeData as $currentKey => $currentData) {
  297. $mobile = $currentData[1] ?? '';
  298. $name = $currentData[0] ?? '';
  299. $address = $currentData[2] ?? '';
  300. if (stringUtil::isMobile($mobile) == false) {
  301. echo "请填写正确的手机号 \n";
  302. continue;
  303. }
  304. if (empty($name)) {
  305. echo "{$mobile} 名称不能为空 \n";
  306. continue;
  307. }
  308. if (stringUtil::getWordNum($name) > 8) {
  309. echo "{$mobile} 名称不能超过8个汉字 \n";
  310. continue;
  311. }
  312. $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  313. if (!empty($has)) {
  314. echo "{$mobile} 名称已经存在 \n";
  315. continue;
  316. }
  317. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  318. if (!empty($has)) {
  319. echo "{$mobile} 手机号已经添加过 \n";
  320. continue;
  321. }
  322. $connection = Yii::$app->db;
  323. $transaction = $connection->beginTransaction();
  324. try {
  325. $sjId = $this->sjId;
  326. $shopId = $this->shopId;
  327. $sjName = $this->sj->name ?? '';
  328. $applyData = [
  329. 'name' => $name,
  330. 'licenseNo' => $mobile,
  331. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  332. 'mobile' => $mobile,
  333. 'introSjId' => $sjId,
  334. 'introSjName' => $sjName,
  335. 'introShopId' => $shopId,
  336. 'introStyle' => SjClass::STYLE_SUPPLIER,
  337. 'from' => ApplyClass::FROM_GHS,
  338. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  339. 'address' => $address,
  340. 'status' => ApplyClass::STATUS_PASS,
  341. 'replace' => 1,//1表示供货商添加的客户或供货商
  342. ];
  343. $apply = ApplyService::replaceKmGhs($applyData);
  344. $id = $apply['id'] ?? 0;
  345. $respond = ApplyService::pass($id);
  346. $currentShop = $respond['shop'] ?? [];
  347. $sj = $respond['sj'] ?? [];
  348. $currentSjId = $sj['id'] ?? 0;
  349. $currentShopId = $currentShop->id ?? 0;
  350. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  351. if (empty($currentShopId)) {
  352. echo "{$mobile} 供货商添加失败 \n ";
  353. continue;
  354. }
  355. GhsClass::build($currentShopId, $shopId, 1);
  356. $transaction->commit();
  357. echo "{$mobile} 供货商添加成功!\n";
  358. } catch (\Exception $exception) {
  359. $transaction->rollBack();
  360. echo "{$mobile} 供货商添加失败,原因:" . $exception->getMessage() . "\n";
  361. }
  362. }
  363. }
  364. }
  365. //客户初始化 ./yii item/custom ./yii.bat item/custom
  366. public function actionCustom()
  367. {
  368. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  369. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  370. $dir = Yii::$app->basePath;
  371. $filename = $dir . '/custom.xls';
  372. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  373. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  374. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  375. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  376. $highestRow = $sheet->getHighestRow();//取得总行数
  377. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  378. $changeData = [];
  379. for ($j = 2; $j <= $highestRow; $j++) {
  380. $str = '';
  381. for ($k = 'A'; $k <= $highestColumn; $k++) {
  382. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  383. }
  384. //explode:函数把字符串分割为数组。
  385. $string = explode("\\", $str);
  386. array_pop($string);
  387. array_push($changeData, $string);
  388. }
  389. if (!empty($changeData)) {
  390. $mobileList = array_values(array_column($changeData, 1));
  391. $ptStyle = dict::getDict('ptStyle', 'hd');
  392. Yii::$app->params['ptStyle'] = $ptStyle;
  393. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  394. if (!empty($has)) {
  395. echo "这些已经添加过了,请先删除:\n";
  396. foreach ($has as $item) {
  397. echo $item->name . ' ' . $item->mobile . "\n";
  398. }
  399. exit();
  400. }
  401. if (getenv('YII_ENV') == 'production') {
  402. $this->sjId = 12367;
  403. $this->shopId = 10;
  404. } else {
  405. $this->sjId = 12615;
  406. $this->shopId = 36119;
  407. }
  408. $shop = ShopClass::getById($this->shopId, true);
  409. $sj = SjClass::getById($this->sjId, true);
  410. $this->sj = $sj;
  411. foreach ($changeData as $currentKey => $currentData) {
  412. $mobile = $currentData[1] ?? '';
  413. $name = $currentData[0] ?? '';
  414. $address = $currentData[2] ?? '';
  415. if (empty($name)) {
  416. echo "{$mobile} 名称不能为空 \n";
  417. continue;
  418. }
  419. if (stringUtil::getWordNum($name) > 8) {
  420. echo "{$mobile} 名称不能超过8个汉字 \n";
  421. continue;
  422. }
  423. if (stringUtil::isMobile($mobile) == false) {
  424. echo "{$mobile} 手机号错误 \n";
  425. continue;
  426. }
  427. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  428. if (!empty($has)) {
  429. echo "{$mobile} 名称已经存在 \n";
  430. continue;
  431. }
  432. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  433. if (!empty($has)) {
  434. echo "{$mobile} 手机号已经添加过 \n";
  435. continue;
  436. }
  437. $connection = Yii::$app->db;
  438. $transaction = $connection->beginTransaction();
  439. try {
  440. $sjId = $this->sjId;
  441. $shopId = $this->shopId;
  442. $sjName = $this->sj->name ?? '';
  443. $city = $shop->city ?? '';
  444. $dist = $shop->dist ?? '';
  445. $applyData = [
  446. 'name' => $name,
  447. 'licenseNo' => $mobile,
  448. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  449. 'mobile' => $mobile,
  450. 'introSjId' => $sjId,
  451. 'introSjName' => $sjName,
  452. 'introShopId' => $shopId,
  453. 'introStyle' => SjClass::STYLE_SUPPLIER,
  454. 'from' => ApplyClass::FROM_GHS,
  455. 'style' => SjClass::STYLE_RETAIL,
  456. 'province' => $shop->province ?? '',
  457. 'city' => $city,
  458. 'dist' => $dist,
  459. 'address' => $address,
  460. 'status' => ApplyClass::STATUS_PASS,
  461. 'replace' => 1,//1表示供货商添加的客户
  462. ];
  463. $apply = ApplyService::replaceRetail($applyData);
  464. $id = $apply['id'] ?? 0;
  465. $respond = ApplyService::pass($id);
  466. $currentShop = $respond['shop'] ?? [];
  467. $sj = $respond['sj'] ?? [];
  468. $hdSjId = $sj['id'] ?? 0;
  469. $hdShopId = $currentShop->id ?? 0;
  470. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  471. if (empty($hdShopId)) {
  472. echo "{$mobile} 客户添加失败 \n";
  473. continue;
  474. }
  475. $custom = CustomClass::build($this->shopId, $hdShopId);
  476. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  477. $transaction->commit();
  478. echo "{$mobile} 添加成功! \n";
  479. } catch (\Exception $exception) {
  480. $transaction->rollBack();
  481. echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
  482. }
  483. }
  484. }
  485. }
  486. }