ItemController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/gys ./yii.bat item/gys
  207. public function actionGys()
  208. {
  209. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  210. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  211. $dir = Yii::$app->basePath;
  212. $filename = $dir . '/gys.xls';
  213. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  214. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  215. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  216. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  217. $highestRow = $sheet->getHighestRow();//取得总行数
  218. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  219. $changeData = [];
  220. for ($j = 2; $j <= $highestRow; $j++) {
  221. $str = '';
  222. for ($k = 'A'; $k <= $highestColumn; $k++) {
  223. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  224. }
  225. //explode:函数把字符串分割为数组。
  226. $string = explode("\\", $str);
  227. array_pop($string);
  228. array_push($changeData, $string);
  229. }
  230. if (!empty($changeData)) {
  231. $mobileList = array_values(array_column($changeData, 1));
  232. $ptStyle = dict::getDict('ptStyle', 'kmGhs');
  233. Yii::$app->params['ptStyle'] = $ptStyle;
  234. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  235. if (!empty($has)) {
  236. echo "这些已经添加过了,请先删除:\n";
  237. foreach ($has as $item) {
  238. echo $item->name . ' ' . $item->mobile . "\n";
  239. }
  240. exit();
  241. }
  242. if (getenv('YII_ENV') == 'production') {
  243. $this->sjId = 12367;
  244. $this->shopId = 10;
  245. } else {
  246. $this->sjId = 12615;
  247. $this->shopId = 36119;
  248. }
  249. foreach ($changeData as $currentKey => $currentData) {
  250. $mobile = $currentData[1] ?? '';
  251. $name = $currentData[0] ?? '';
  252. $address = $currentData[2] ?? '';
  253. if (stringUtil::isMobile($mobile) == false) {
  254. echo "请填写正确的手机号 \n";
  255. continue;
  256. }
  257. if (empty($name)) {
  258. echo "{$mobile} 名称不能为空 \n";
  259. continue;
  260. }
  261. if (stringUtil::getWordNum($name) > 8) {
  262. echo "{$mobile} 名称不能超过8个汉字 \n";
  263. continue;
  264. }
  265. $has = ApplyClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  266. if (!empty($has)) {
  267. echo "{$mobile} 名称已经存在 \n";
  268. continue;
  269. }
  270. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_KM_SUPPLIER]);
  271. if (!empty($has)) {
  272. echo "{$mobile} 手机号已经添加过 \n";
  273. continue;
  274. }
  275. $connection = Yii::$app->db;
  276. $transaction = $connection->beginTransaction();
  277. try {
  278. $sjId = $this->sjId;
  279. $shopId = $this->shopId;
  280. $sjName = $this->sj->name ?? '';
  281. $applyData = [
  282. 'name' => $name,
  283. 'licenseNo' => $mobile,
  284. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  285. 'mobile' => $mobile,
  286. 'introSjId' => $sjId,
  287. 'introSjName' => $sjName,
  288. 'introShopId' => $shopId,
  289. 'introStyle' => SjClass::STYLE_SUPPLIER,
  290. 'from' => ApplyClass::FROM_GHS,
  291. 'style' => dict::getDict('ptStyle', 'kmGhs'),
  292. 'address' => $address,
  293. 'status' => ApplyClass::STATUS_PASS,
  294. 'replace' => 1,//1表示供应商添加的客户或供应商
  295. ];
  296. $apply = ApplyService::replaceKmGhs($applyData);
  297. $id = $apply['id'] ?? 0;
  298. $respond = ApplyService::pass($id);
  299. $currentShop = $respond['shop'] ?? [];
  300. $sj = $respond['sj'] ?? [];
  301. $currentSjId = $sj['id'] ?? 0;
  302. $currentShopId = $currentShop->id ?? 0;
  303. ApplyClass::updateById($id, ['sjId' => $currentSjId, 'shopId' => $currentShopId]);
  304. if (empty($currentShopId)) {
  305. echo "{$mobile} 供应商添加失败 \n ";
  306. continue;
  307. }
  308. GhsClass::build($currentShopId, $shopId, 1);
  309. $transaction->commit();
  310. echo "{$mobile} 供应商添加成功!\n";
  311. } catch (\Exception $exception) {
  312. $transaction->rollBack();
  313. echo "{$mobile} 供应商添加失败,原因:" . $exception->getMessage() . "\n";
  314. }
  315. }
  316. }
  317. }
  318. //客户初始化 ./yii item/custom ./yii.bat item/custom
  319. public function actionCustom()
  320. {
  321. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  322. require_once($phpExcelFile . 'Classes/PHPExcel/IOFactory.php');//查询接口
  323. $dir = Yii::$app->basePath;
  324. $filename = $dir . '/custom.xls';
  325. $fileType = \PHPExcel_IOFactory::identify($filename); //文件名自动判断文件类型
  326. $excelReader = \PHPExcel_IOFactory::createReader($fileType);
  327. $objPHPExcel = $excelReader->load($filename);//$file_url即Excel文件的路径
  328. $sheet = $objPHPExcel->getSheet(0);//获取第一个工作表
  329. $highestRow = $sheet->getHighestRow();//取得总行数
  330. $highestColumn = $sheet->getHighestColumn(); //取得总列数
  331. $changeData = [];
  332. for ($j = 2; $j <= $highestRow; $j++) {
  333. $str = '';
  334. for ($k = 'A'; $k <= $highestColumn; $k++) {
  335. $str .= $objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue() . '\\';
  336. }
  337. //explode:函数把字符串分割为数组。
  338. $string = explode("\\", $str);
  339. array_pop($string);
  340. array_push($changeData, $string);
  341. }
  342. if (!empty($changeData)) {
  343. $mobileList = array_values(array_column($changeData, 1));
  344. $ptStyle = dict::getDict('ptStyle', 'hd');
  345. Yii::$app->params['ptStyle'] = $ptStyle;
  346. $has = Apply::find()->where(['in', 'mobile', $mobileList])->andWhere(['style' => $ptStyle])->all();
  347. if (!empty($has)) {
  348. echo "这些已经添加过了,请先删除:\n";
  349. foreach ($has as $item) {
  350. echo $item->name . ' ' . $item->mobile . "\n";
  351. }
  352. exit();
  353. }
  354. if (getenv('YII_ENV') == 'production') {
  355. $this->sjId = 12367;
  356. $this->shopId = 10;
  357. } else {
  358. $this->sjId = 12615;
  359. $this->shopId = 36119;
  360. }
  361. $shop = ShopClass::getById($this->shopId, true);
  362. $sj = SjClass::getById($this->sjId, true);
  363. $this->sj = $sj;
  364. foreach ($changeData as $currentKey => $currentData) {
  365. $mobile = $currentData[1] ?? '';
  366. $name = $currentData[0] ?? '';
  367. $address = $currentData[2] ?? '';
  368. if (empty($name)) {
  369. echo "{$mobile} 名称不能为空 \n";
  370. continue;
  371. }
  372. if (stringUtil::getWordNum($name) > 8) {
  373. echo "{$mobile} 名称不能超过8个汉字 \n";
  374. continue;
  375. }
  376. if (stringUtil::isMobile($mobile) == false) {
  377. echo "{$mobile} 手机号错误 \n";
  378. continue;
  379. }
  380. $has = SjClass::getByCondition(['name' => $name, 'style' => SjClass::STYLE_RETAIL]);
  381. if (!empty($has)) {
  382. echo "{$mobile} 名称已经存在 \n";
  383. continue;
  384. }
  385. $has = ApplyClass::getByCondition(['mobile' => $mobile, 'style' => SjClass::STYLE_RETAIL]);
  386. if (!empty($has)) {
  387. echo "{$mobile} 手机号已经添加过 \n";
  388. continue;
  389. }
  390. $connection = Yii::$app->db;
  391. $transaction = $connection->beginTransaction();
  392. try {
  393. $sjId = $this->sjId;
  394. $shopId = $this->shopId;
  395. $sjName = $this->sj->name ?? '';
  396. $city = $shop->city ?? '';
  397. $dist = $shop->dist ?? '';
  398. $applyData = [
  399. 'name' => $name,
  400. 'licenseNo' => $mobile,
  401. 'certType' => ApplyClass::CERT_TYPE_MOBILE,
  402. 'mobile' => $mobile,
  403. 'introSjId' => $sjId,
  404. 'introSjName' => $sjName,
  405. 'introShopId' => $shopId,
  406. 'introStyle' => SjClass::STYLE_SUPPLIER,
  407. 'from' => ApplyClass::FROM_GHS,
  408. 'style' => SjClass::STYLE_RETAIL,
  409. 'province' => $shop->province ?? '',
  410. 'city' => $city,
  411. 'dist' => $dist,
  412. 'address' => $address,
  413. 'status' => ApplyClass::STATUS_PASS,
  414. 'replace' => 1,//1表示供应商添加的客户
  415. ];
  416. $apply = ApplyService::replaceRetail($applyData);
  417. $id = $apply['id'] ?? 0;
  418. $respond = ApplyService::pass($id);
  419. $currentShop = $respond['shop'] ?? [];
  420. $sj = $respond['sj'] ?? [];
  421. $hdSjId = $sj['id'] ?? 0;
  422. $hdShopId = $currentShop->id ?? 0;
  423. ApplyClass::updateById($id, ['sjId' => $hdSjId, 'shopId' => $hdShopId]);
  424. if (empty($hdShopId)) {
  425. echo "{$mobile} 客户添加失败 \n";
  426. continue;
  427. }
  428. $custom = CustomClass::build($this->shopId, $hdShopId);
  429. $custom['avatar'] = imgUtil::groupImg($custom['avatar']);
  430. $transaction->commit();
  431. echo "{$mobile} 添加成功! \n";
  432. } catch (\Exception $exception) {
  433. $transaction->rollBack();
  434. echo "{$mobile} 添加失败,报错:" . $exception->getMessage() . "\n";
  435. }
  436. }
  437. }
  438. }
  439. }