ItemController.php 21 KB

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