ItemController.php 21 KB

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