ItemController.php 19 KB

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