ItemController.php 21 KB

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