ItemController.php 19 KB

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