ProductController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. <?php
  2. /**
  3. * User: admin
  4. * Date Time: 2021/1/15 20:36
  5. */
  6. namespace ghs\controllers;
  7. use biz\admin\classes\AdminRoleClass;
  8. use biz\ghs\classes\GhsClass;
  9. use biz\item\classes\PtItemClass;
  10. use bizGhs\custom\classes\CustomClass;
  11. use bizGhs\item\classes\ItemClassClass;
  12. use bizGhs\merchant\classes\ShopClass;
  13. use bizGhs\product\classes\ProductClass;
  14. use bizGhs\product\models\Product;
  15. use bizGhs\product\services\ProductService;
  16. use common\components\stringUtil;
  17. use common\components\util;
  18. use Yii;
  19. class ProductController extends BaseController
  20. {
  21. public $guestAccess = ['index'];
  22. //按分类列出所有花材
  23. public function actionIndex()
  24. {
  25. $py = Yii::$app->request->get('py', '');
  26. $requestType = Yii::$app->request->get('requestType', 'common');
  27. //默认显示上架商品
  28. $status = Yii::$app->request->get('status', 1);
  29. //默认显示未删除商品
  30. $delStatus = Yii::$app->request->get('delStatus', 0);
  31. //获取所有当前供应商的分类 (全部、常用)
  32. //根据分类组装分类下的花材信息
  33. //中央ID
  34. $classIds = ItemClassClass::getGhsItemClassAll($this->mainId);
  35. $where['mainId'] = $this->mainId;
  36. if ($py) {
  37. $pyName = strtolower($py);
  38. $where['py'] = $pyName;
  39. }
  40. if ($requestType == 'changePrice') {
  41. }
  42. $where['stock>'] = 0;
  43. if (!empty($status)) {
  44. $where['status'] = $status;
  45. }
  46. $where['delStatus'] = $delStatus;
  47. //客户等级
  48. $customId = Yii::$app->request->get('customId', 0);
  49. $level = 1;
  50. if (!empty($customId)) {
  51. $custom = CustomClass::getById($customId, true);
  52. $level = $custom->level ?? 1;
  53. }
  54. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  55. $itemInfoData = ProductClass::groupProductInfo($itemInfoData, $level);
  56. $data = ProductService::assembleData($classIds, $itemInfoData, true);
  57. util::success($data);
  58. }
  59. //所有花材分页和不分页
  60. public function actionList()
  61. {
  62. $where = [];
  63. $classId = Yii::$app->request->get('classId', 0);
  64. if (!empty($classId)) {
  65. $where['classId'] = $classId;
  66. }
  67. $name = Yii::$app->request->get('name', '');
  68. if (empty($name)) {
  69. $py = Yii::$app->request->get('py', '');
  70. $name = !empty($py) ? $py : '';
  71. }
  72. if (!empty($name)) {
  73. if (stringUtil::isLetter($name)) {
  74. $where['py'] = ['like', $name];
  75. } else {
  76. $where['name'] = ['like', $name];
  77. }
  78. }
  79. $status = Yii::$app->request->get('status', 0);
  80. if (!empty($status)) {
  81. $where['status'] = $status;
  82. }
  83. $delStatus = Yii::$app->request->get('delStatus', 0);
  84. if ($delStatus != 2) {
  85. $where['delStatus'] = $delStatus;
  86. }
  87. $where['mainId'] = $this->mainId;
  88. $productIds = Yii::$app->request->get('ids', '');
  89. if (!empty($productIds)) {
  90. $productIdArr = explode(',', $productIds);
  91. if (!empty($productIdArr)) {
  92. $where['id'] = ['in', $productIdArr];
  93. }
  94. }
  95. //输出标准会员价,即花店价,默认显示的是花店价
  96. $level = 1;
  97. $type = Yii::$app->request->get('type', '');
  98. $showPage = Yii::$app->request->get('showPage', 0);
  99. if ($showPage) {
  100. if ($type == 'waring') {
  101. $pro = Product::find()->where("mainId={$this->mainId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  102. $lackIds = array_column($pro, 'id');
  103. if (empty($lackIds)) {
  104. util::success([]);
  105. }
  106. $lackIds = array_filter(array_unique($lackIds));
  107. $where['id'] = ['in', $lackIds];
  108. $respond = ProductClass::getList("*", $where, "actualSold desc");
  109. } else {
  110. $respond = ProductClass::getList("*", $where, "inTurn desc");
  111. }
  112. $respond['list'] = ProductClass::groupProductInfo($respond['list'], $level);
  113. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  114. util::success($respond);
  115. } else {
  116. if ($type == 'waring') {
  117. //库存预警 按库存从小到大排序
  118. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  119. } else {
  120. if (!empty($productIds)) {
  121. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  122. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  123. } else {
  124. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  125. }
  126. }
  127. $data = ProductClass::groupProductInfo($data, $level);
  128. $data = ProductClass::groupClassName($this->sjId, $data);
  129. if ($type == 'waring') {
  130. //库存预警
  131. $newData = [];
  132. foreach ($data as $v) {
  133. if ($v['stock'] <= $v['stockWarning']) {
  134. $newData[] = $v;
  135. }
  136. }
  137. util::success(['list' => $newData]);
  138. }
  139. util::success(['list' => $data]);
  140. }
  141. }
  142. //所有花材
  143. public function actionAll()
  144. {
  145. $where = [];
  146. $where['mainId'] = $this->mainId;
  147. $classId = Yii::$app->request->get('classId', 0);
  148. if (!empty($classId)) {
  149. $where['classId'] = $classId;
  150. }
  151. $status = Yii::$app->request->get('status', 0);
  152. if (!empty($status)) {
  153. $where['status'] = $status;
  154. }
  155. $delStatus = Yii::$app->request->get('delStatus', 0);
  156. if ($delStatus != 2) {
  157. $where['delStatus'] = $delStatus;
  158. }
  159. //输出标准会员价,即花店价,默认显示的是花店价
  160. $level = 1;
  161. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  162. $data = ProductClass::groupProductInfo($data, $level);
  163. util::success(['list' => $data]);
  164. }
  165. //批量改价
  166. public function actionBatchChangePrice()
  167. {
  168. $shop = $this->shop;
  169. $join = $shop->join ?? 0;
  170. $default = $shop->default ?? 0;
  171. if ($join == 0 && $default == 0) {
  172. util::fail('请在总店操作');
  173. }
  174. $shopAdmin = $this->shopAdmin;
  175. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  176. util::fail('超管才能修改');
  177. }
  178. $shop = $this->shop;
  179. $default = $shop->default ?? 0;
  180. $data = Yii::$app->request->post('data', '');
  181. if (empty($data)) {
  182. util::fail('没有修改');
  183. }
  184. $arr = json_decode($data, true);
  185. if (empty($arr)) {
  186. util::fail('没有修改哦');
  187. }
  188. $connection = Yii::$app->db;
  189. $transaction = $connection->beginTransaction();
  190. try {
  191. $ids = array_column($arr, 'id');
  192. $ids = array_unique(array_filter($ids));
  193. if (empty($ids)) {
  194. util::fail('请选择花材');
  195. }
  196. $sjId = $shop->sjId ?? 0;
  197. $chainShopList = [];
  198. if ($default == 1) {
  199. $chainShopList = ShopClass::getAllByCondition(['sjId' => $sjId, 'join' => 0], null, '*', null, true);
  200. }
  201. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  202. foreach ($arr as $product) {
  203. if (isset($product['price']) == false) {
  204. util::fail('没有价格');
  205. }
  206. if (isset($product['id']) == false) {
  207. util::fail('没有花材');
  208. }
  209. $price = $product['price'];
  210. $productId = $product['id'];
  211. $product = $productList[$productId] ?? [];
  212. if (empty($product)) {
  213. util::fail('没有找到花材');
  214. }
  215. if (isset($product->mainId) == false || $product->mainId != $this->mainId) {
  216. util::fail('没有权限访问');
  217. }
  218. if (empty($price)) {
  219. continue;
  220. }
  221. $ptItemId = $product->itemId;
  222. ProductClass::changeSinglePrice($shop, $ptItemId, $price);
  223. //在首店修改需要同步到所有直营店
  224. if ($default == 1) {
  225. foreach ($chainShopList as $chain) {
  226. if ($chain->id == $shop->id) {
  227. continue;
  228. }
  229. ProductClass::changeSinglePrice($chain, $ptItemId, $price);
  230. }
  231. }
  232. }
  233. $transaction->commit();
  234. util::complete('修改成功');
  235. } catch (\Exception $e) {
  236. $transaction->rollBack();
  237. Yii::info("修改失败原因:" . $e->getMessage());
  238. util::fail('修改失败');
  239. }
  240. }
  241. //改价
  242. public function actionChangePrice()
  243. {
  244. $shopAdmin = $this->shopAdmin;
  245. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  246. util::fail('超管才能修改');
  247. }
  248. $productId = Yii::$app->request->post('productId', 0);
  249. $productInfo = ProductClass::getById($productId, true);
  250. if (empty($productInfo)) {
  251. util::fail("花材不存在");
  252. }
  253. if ($productInfo->mainId != $this->mainId) {
  254. util::fail('没有权限');
  255. }
  256. $price = Yii::$app->request->post('price', '');
  257. if (is_numeric($price) && $price > 0) {
  258. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  259. } else {
  260. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  261. ProductClass::changeSinglePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  262. }
  263. util::success(['price' => $price]);
  264. }
  265. //恢复自动调价
  266. public function actionAutoPrice()
  267. {
  268. $productId = Yii::$app->request->post('productId', 0);
  269. $productData = ProductClass::getById($productId);
  270. if (!$productData) {
  271. util::fail("花材不存在");
  272. }
  273. //2021.04.06改为用product中的成本价+加价
  274. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  275. ProductClass::changeSinglePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  276. util::complete("操作成功");
  277. }
  278. //修改加价
  279. public function actionChangeAddPrice()
  280. {
  281. $productId = Yii::$app->request->post('productId', 0);
  282. $addPrice = Yii::$app->request->post('addPrice', '');
  283. if ($addPrice < 0) {
  284. util::fail("加价不能小于0");
  285. }
  286. $productData = ProductClass::getProductData($productId, $this->mainId);
  287. if (!$productData) {
  288. util::fail("花材不存在");
  289. }
  290. ProductClass::changeAddPrice($productId, $addPrice);
  291. util::complete();
  292. }
  293. //批量修改更多,包括排序、重量和保质期 ssh 20211211
  294. public function actionBatchChangeMore()
  295. {
  296. $shop = $this->shop;
  297. $join = $shop->join ?? 0;
  298. $default = $shop->default ?? 0;
  299. if ($join == 0 && $default == 0) {
  300. util::fail('请在总店操作');
  301. }
  302. $shopAdmin = $this->shopAdmin;
  303. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  304. util::fail('超管才能操作');
  305. }
  306. $addData = Yii::$app->request->post('addData', '');
  307. if (empty($addData)) {
  308. util::fail('没有花材');
  309. }
  310. $itemData = json_decode($addData, true);
  311. if (is_array($itemData) == false) {
  312. util::fail('没有花材...');
  313. }
  314. $ids = array_column($itemData, 'id');
  315. $infoList = ProductClass::getByIds($ids);
  316. if (empty($infoList)) {
  317. util::fail('没有花材');
  318. }
  319. foreach ($infoList as $info) {
  320. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  321. util::fail('非常访问,不是你的花材不能修改');
  322. }
  323. }
  324. foreach ($itemData as $key => $data) {
  325. $id = $data['id'] ?? 0;
  326. ProductClass::updateById($id, $data);
  327. }
  328. util::complete('修改成功');
  329. }
  330. //批量修改加价 ssh 20211211
  331. public function actionBatchChangeAddPrice()
  332. {
  333. $shop = $this->shop;
  334. $join = $shop->join ?? 0;
  335. $default = $shop->default ?? 0;
  336. if ($join == 0 && $default == 0) {
  337. util::fail('请在总店操作');
  338. }
  339. $shopAdmin = $this->shopAdmin;
  340. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  341. util::fail('超管才能操作');
  342. }
  343. $addData = Yii::$app->request->post('addData', '');
  344. if (empty($addData)) {
  345. util::fail('没有花材');
  346. }
  347. $itemData = json_decode($addData, true);
  348. if (is_array($itemData) == false) {
  349. util::fail('没有花材...');
  350. }
  351. $ids = array_column($itemData, 'id');
  352. $infoList = ProductClass::getByIds($ids);
  353. if (empty($infoList)) {
  354. util::fail('没有花材');
  355. }
  356. foreach ($infoList as $info) {
  357. if (isset($info['mainId']) == false || $info['mainId'] != $this->mainId) {
  358. util::fail('非常访问,不是你的花材不能修改');
  359. }
  360. }
  361. foreach ($itemData as $key => $data) {
  362. $id = $data['id'] ?? 0;
  363. ProductClass::updateById($id, $data);
  364. }
  365. util::complete('修改成功');
  366. }
  367. public function actionUpdate()
  368. {
  369. $shop = $this->shop;
  370. $join = $shop->join ?? 0;
  371. $default = $shop->default ?? 0;
  372. if ($join == 0 && $default == 0) {
  373. util::fail('请在总店操作');
  374. }
  375. $shopAdmin = $this->shopAdmin;
  376. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  377. util::fail('超管才能操作');
  378. }
  379. $post = Yii::$app->request->post();
  380. $post['adminId'] = $this->adminId;
  381. $post['staffName'] = $this->shopAdmin->name;
  382. ProductClass::updateProduct($post, $this->shop);
  383. util::complete();
  384. }
  385. //上下架状态控制 ssh 20210713
  386. public function actionStatusUpdate()
  387. {
  388. $post = Yii::$app->request->post();
  389. $shopAdmin = $this->shopAdmin;
  390. $roleId = $shopAdmin['roleId'] ?? 0;
  391. $role = AdminRoleClass::getById($roleId);
  392. $roleName = $role['roleName'] ?? '';
  393. if ($roleName == '员工') {
  394. util::fail('没有权限操作哦');
  395. }
  396. $id = $post['id'] ?? 0;
  397. $status = $post['status'] ?? 1;
  398. $product = ProductClass::getById($id, true);
  399. if ($product->mainId != $this->mainId) {
  400. util::fail('没有权限操作');
  401. }
  402. $product->status = $status;
  403. if ($status == 2) {
  404. $product->discountPrice = 0;
  405. }
  406. $product->save();
  407. util::complete();
  408. }
  409. //花材展示状态控制 ssh 20210804
  410. public function actionDelStatusUpdate()
  411. {
  412. $post = Yii::$app->request->post();
  413. $shopAdmin = $this->shopAdmin;
  414. $roleId = $shopAdmin['roleId'] ?? 0;
  415. $role = AdminRoleClass::getById($roleId);
  416. $roleName = $role['roleName'] ?? '';
  417. if ($roleName == '员工') {
  418. util::fail('没有权限操作');
  419. }
  420. $id = $post['id'] ?? 0;
  421. $delStatus = $post['delStatus'] ?? 0;
  422. $product = ProductClass::getById($id, true);
  423. if ($product->mainId != $this->mainId) {
  424. util::fail('没有权限操作');
  425. }
  426. $product->delStatus = $delStatus;
  427. $product->save();
  428. util::complete();
  429. }
  430. //详情 ruidian 2021.5.3
  431. public function actionDetail()
  432. {
  433. $id = Yii::$app->request->get('id', 0);
  434. $respond = ProductClass::getItemInfo($id);
  435. $ptItemId = $respond['itemId'] ?? 0;
  436. $ptItemInfo = PtItemClass::getById($ptItemId);
  437. $respond['ptItemInfo'] = $ptItemInfo;
  438. util::success($respond);
  439. }
  440. //新增花材 2021.6.21
  441. public function actionAdd()
  442. {
  443. $post = Yii::$app->request->post();
  444. $shop = $this->shop;
  445. $join = $shop->join ?? 0;
  446. $default = $shop->default ?? 0;
  447. if ($join == 0 && $default == 0) {
  448. util::fail('请在总店操作');
  449. }
  450. $shopAdmin = $this->shopAdmin;
  451. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  452. util::fail('管理员才能添加花材');
  453. }
  454. $post['sjId'] = $this->sjId;
  455. $post['adminId'] = $this->adminId;
  456. $post['mainId'] = $this->mainId;
  457. $price = $post['price'] ?? 0;
  458. $addPrice = $post['addPrice'] ?? 0;
  459. if ($addPrice > $price) {
  460. util::fail('加价不能大于售价');
  461. }
  462. $cost = bcsub($price, $addPrice, 2);
  463. $post['cost'] = $cost;
  464. ProductClass::addProduct($post, $this->shop);
  465. util::complete('添加成功');
  466. }
  467. //获取拼音缩写 ssh 20210707
  468. public function actionPy()
  469. {
  470. $name = Yii::$app->request->get('name');
  471. $py = stringUtil::py($name);
  472. util::success(['py' => $py]);
  473. }
  474. //(客户端-批发商)复制花材
  475. public function actionClone()
  476. {
  477. $post = Yii::$app->request->post();
  478. $shop = $this->shop;
  479. $join = $shop->join ?? 0;
  480. $default = $shop->default ?? 0;
  481. if ($join == 0 && $default == 0) {
  482. util::fail('请在总店操作');
  483. }
  484. $shopAdmin = $this->shopAdmin;
  485. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  486. util::fail('管理员才能添加花材');
  487. }
  488. $shop = $this->shop;
  489. $default = $shop->default ?? 0;
  490. if ($default != 1) {
  491. util::fail('请在总店添加');
  492. }
  493. $post['sjId'] = $this->sjId;
  494. $post['adminId'] = $this->adminId;
  495. $post['mainId'] = $this->mainId;
  496. $price = $post['price'] ?? 0;
  497. $addPrice = $post['addPrice'] ?? 0;
  498. if ($addPrice > $price) {
  499. util::fail('加价不能大于售价');
  500. }
  501. $cost = bcsub($price, $addPrice, 2);
  502. $post['cost'] = $cost;
  503. $post['copy'] = 1; //复制标识
  504. //去除无用的字段
  505. unset($post['viewNum']);
  506. unset($post['actualSold']);
  507. unset($post['itemId']);
  508. unset($post['id']);
  509. unset($post['delStatus']);
  510. ProductClass::addProduct($post, $this->shop);
  511. util::complete('复制成功');
  512. }
  513. //下载价格表 ssh 20210922
  514. public function actionGeneratePriceTable()
  515. {
  516. $level = Yii::$app->request->get('level', 0);
  517. $respond = ProductService::generatePriceTable($this->sjId, $level, $this->mainId);
  518. $file = $respond['file'] ?? '';
  519. $shortFile = $respond['shortFile'] ?? '';
  520. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  521. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  522. }
  523. //下载条形码 lqh 20211227
  524. public function actionGenerateBarcodeTable()
  525. {
  526. $respond = ProductService::generateBarcodeTable($this->sjId, $this->shopId, $this->mainId);
  527. $file = $respond['file'] ?? '';
  528. $shortFile = $respond['shortFile'] ?? '';
  529. $fileUrl = Yii::$app->params['ghsHost'] . $file;
  530. util::success(['file' => $fileUrl, 'shortFile' => $shortFile]);
  531. }
  532. }