ItemController.php 22 KB

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