ItemController.php 20 KB

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