ItemController.php 21 KB

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