ItemController.php 19 KB

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