ItemController.php 20 KB

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