ItemController.php 19 KB

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