ItemController.php 21 KB

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