ItemController.php 22 KB

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