ProductController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 bizGhs\item\classes\ItemClassClass;
  9. use bizGhs\merchant\classes\ShopClass;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\product\models\Product;
  12. use bizGhs\product\services\ProductService;
  13. use common\components\stringUtil;
  14. use common\components\util;
  15. use Yii;
  16. class ProductController extends BaseController
  17. {
  18. public $guestAccess = ['index'];
  19. //开单时获取所有的分类花材信息
  20. //库存预警
  21. public function actionIndex()
  22. {
  23. $py = Yii::$app->request->get('py', '');
  24. $type = Yii::$app->request->get('type', '');
  25. //默认只显示上架的商品
  26. $status = Yii::$app->request->get('status', 1);
  27. //获取所有当前供应商的分类 (全部、常用)
  28. //根据分类组装分类下的花材信息
  29. $classIds = ItemClassClass::getGhsItemClassAll($this->sjId);
  30. $where['sjId'] = $this->sjId;
  31. if ($py) {
  32. $pyName = strtolower($py);
  33. $where['py'] = $pyName;
  34. }
  35. $shopId = $this->shopId;
  36. //兼容客户根据门店下单
  37. if ($this->lookShopId) {
  38. $shopId = $this->lookShopId;
  39. }
  40. //库存预警
  41. if ($type == 'waring') {
  42. //shopId 改为可以前端传,默认是当前shopId linqh 2021.1.26
  43. $shopId = Yii::$app->request->get('shopId', 0);
  44. if (!$shopId) {
  45. $shopId = $this->shopId;
  46. }
  47. }
  48. $where['shopId'] = $shopId;
  49. if (!empty($status)) {
  50. $where['status'] = $status;
  51. }
  52. $where['delStatus'] = 0;
  53. $itemInfoData = ProductClass::getAllByCondition($where, ['inTurn' => SORT_DESC, 'actualSold' => SORT_DESC], "*");
  54. $itemInfoData = ProductClass::groupProductInfo($itemInfoData);
  55. //常用的itemIds
  56. $oftenItemIds = [];
  57. //$oftenItemIds = ProductClass::getOftenItemIds($shopId);
  58. //获取供应商下所有花材ID (itemIds)
  59. $itemIdsInfo = ProductClass::getGhsItemIds($this->sjId);
  60. //dd($itemInfoData);
  61. //组装数据: [{classId:'','className:'',child:[{itemInfoData}]}]
  62. $data = ProductService::assembleData($classIds, $itemIdsInfo, $itemInfoData, $oftenItemIds, $type);
  63. util::success($data);
  64. }
  65. //当前门店所有花材
  66. public function actionList()
  67. {
  68. $where = [];
  69. $where['sjId'] = $this->sjId;
  70. $classId = Yii::$app->request->get('classId', 0);
  71. if (!empty($classId)) {
  72. $where['classId'] = $classId;
  73. }
  74. $name = Yii::$app->request->get('name', '');
  75. if (empty($name)) {
  76. $py = Yii::$app->request->get('py', '');
  77. $name = !empty($py) ? $py : '';
  78. }
  79. if (!empty($name)) {
  80. if (stringUtil::isLetter($name)) {
  81. $where['py'] = ['like', $name];
  82. } else {
  83. $where['name'] = ['like', $name];
  84. }
  85. }
  86. $status = Yii::$app->request->get('status', 0);
  87. if (!empty($status)) {
  88. $where['status'] = $status;
  89. }
  90. $delStatus = Yii::$app->request->get('delStatus', '');
  91. if (strlen($delStatus) > 0 && is_numeric($delStatus)) {
  92. $where['delStatus'] = $delStatus;
  93. }
  94. $shopId = Yii::$app->request->get('shopId', 0);
  95. if (!$shopId) {
  96. $shopId = $this->shopId;
  97. }
  98. $where['shopId'] = $shopId;
  99. $productIds = Yii::$app->request->get('ids', '');
  100. if (!empty($productIds)) {
  101. $productIdArr = explode(',', $productIds);
  102. if (!empty($productIdArr)) {
  103. $where['id'] = ['in', $productIdArr];
  104. }
  105. }
  106. $type = Yii::$app->request->get('type', '');
  107. $showPage = Yii::$app->request->get('showPage', 0);
  108. if ($showPage) {
  109. if ($type == 'waring') {
  110. $pro = Product::find()->where("shopId={$shopId}")->andWhere('`stock`<`stockWarning`')->select('id')->asArray()->all();
  111. $lackIds = array_column($pro, 'id');
  112. if (empty($lackIds)) {
  113. util::success([]);
  114. }
  115. $lackIds = array_filter(array_unique($lackIds));
  116. $where['id'] = ['in', $lackIds];
  117. $respond = ProductClass::getList("*", $where, "stock asc");
  118. } else {
  119. $respond = ProductClass::getList("*", $where, "inTurn desc");
  120. }
  121. $respond['list'] = ProductClass::groupProductInfo($respond['list']);
  122. $respond['list'] = ProductClass::groupClassName($this->sjId, $respond['list']);
  123. util::success($respond);
  124. }
  125. if ($type == 'waring') {
  126. //库存预警 按库存从小到大排序
  127. $data = ProductClass::getAllByCondition($where, ['stock' => SORT_ASC, 'inTurn' => SORT_DESC], "*");
  128. } else {
  129. if(!empty($productIds)){
  130. //优先按 $productIds 排序 "FIELD(`id`,4,5,6)"=>true
  131. $data = ProductClass::getAllByCondition($where, ["FIELD(`id`,$productIds)" => true, 'inTurn' => SORT_DESC], "*");
  132. }else{
  133. $data = ProductClass::getAllByCondition($where, ['actualSold' => SORT_DESC, 'inTurn' => SORT_DESC], "*");
  134. }
  135. }
  136. $data = ProductClass::groupProductInfo($data);
  137. $data = ProductClass::groupClassName($this->sjId, $data);
  138. if ($type == 'waring') {
  139. //库存预警
  140. $newData = [];
  141. foreach ($data as $v) {
  142. if ($v['stock'] <= $v['stockWarning']) {
  143. $newData[] = $v;
  144. }
  145. }
  146. util::success(['list' => $newData]);
  147. }
  148. util::success(['list' => $data]);
  149. }
  150. //批量改价
  151. public function actionBatchChangePrice()
  152. {
  153. $shopAdmin = $this->shopAdmin;
  154. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  155. util::fail('超管才能修改');
  156. }
  157. //花材价格只能在首店修改,并同步到分店
  158. $shop = $this->shop;
  159. $default = $shop->default ?? 0;
  160. if ($default != 1) {
  161. util::fail('请在首店修改');
  162. }
  163. $data = Yii::$app->request->post('data', '');
  164. if (empty($data)) {
  165. util::fail('没有修改');
  166. }
  167. $arr = json_decode($data, true);
  168. if (empty($arr)) {
  169. util::fail('没有修改!');
  170. }
  171. $connection = Yii::$app->db;
  172. $transaction = $connection->beginTransaction();
  173. try {
  174. $ids = array_column($arr, 'id');
  175. $ids = array_unique(array_filter($ids));
  176. if (empty($ids)) {
  177. util::fail('请选择花材');
  178. }
  179. $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  180. foreach ($arr as $product) {
  181. if (isset($product['price']) == false) {
  182. util::fail('没有价格');
  183. }
  184. if (isset($product['id']) == false) {
  185. util::fail('没有花材');
  186. }
  187. $price = $product['price'];
  188. $productId = $product['id'];
  189. $product = $productList[$productId] ?? [];
  190. if (empty($product)) {
  191. util::fail('没有找到花材');
  192. }
  193. if (isset($product->shopId) == false || $product->shopId != $this->shopId) {
  194. util::fail('没有权限访问');
  195. }
  196. if (empty($price)) {
  197. //恢复自动调价
  198. $price = bcadd($product->cost, $product->addPrice, 2);
  199. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_AUTO);
  200. } else {
  201. //改价
  202. ProductClass::changePrice($product, $price, ProductClass::PRICE_LABEL_CHANGE);
  203. }
  204. }
  205. $transaction->commit();
  206. util::complete('修改成功');
  207. } catch (\Exception $e) {
  208. $transaction->rollBack();
  209. Yii::info("修改失败原因:" . $e->getMessage());
  210. util::fail('修改失败');
  211. }
  212. }
  213. //改价
  214. public function actionChangePrice()
  215. {
  216. $shopAdmin = $this->shopAdmin;
  217. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  218. util::fail('超管才能修改');
  219. }
  220. //花材价格只能在首店修改,并同步到分店
  221. $shop = $this->shop;
  222. $default = $shop->default ?? 0;
  223. if ($default != 1) {
  224. util::fail('请在首店修改');
  225. }
  226. $productId = Yii::$app->request->post('productId', 0);
  227. $productInfo = ProductClass::getById($productId, true);
  228. if (empty($productInfo)) {
  229. util::fail("花材不存在");
  230. }
  231. if ($productInfo->shopId != $this->shopId) {
  232. util::fail('没有权限');
  233. }
  234. $price = Yii::$app->request->post('price', '');
  235. if (is_numeric($price) && $price > 0) {
  236. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_CHANGE);
  237. } else {
  238. $price = bcadd($productInfo->cost, $productInfo->addPrice, 2);
  239. ProductClass::changePrice($productInfo, $price, ProductClass::PRICE_LABEL_AUTO);
  240. }
  241. util::success(['price' => $price]);
  242. }
  243. //恢复自动调价
  244. public function actionAutoPrice()
  245. {
  246. $productId = Yii::$app->request->post('productId', 0);
  247. $productData = ProductClass::getById($productId);
  248. if (!$productData) {
  249. util::fail("花材不存在");
  250. }
  251. //2021.04.06改为用product中的成本价+加价
  252. $price = bcadd($productData['cost'], $productData['addPrice'], 2);
  253. ProductClass::changePrice($productId, $this->shopId, $price, ProductClass::PRICE_LABEL_AUTO);
  254. util::complete("操作成功");
  255. }
  256. //修改加价
  257. public function actionChangeAddPrice()
  258. {
  259. $productId = Yii::$app->request->post('productId', 0);
  260. $addPrice = Yii::$app->request->post('addPrice', '');
  261. if ($addPrice < 0) {
  262. util::fail("加价不能小于0");
  263. }
  264. $productData = ProductClass::getProductData($productId, $this->shopId);
  265. if (!$productData) {
  266. util::fail("花材不存在");
  267. }
  268. ProductClass::changeAddPrice($productId, $addPrice);
  269. util::complete();
  270. }
  271. //批量修改product 排序,成本价,加价
  272. public function actionBatchUpdate()
  273. {
  274. $shopAdmin = $this->shopAdmin;
  275. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  276. util::fail('超管才能修改');
  277. }
  278. //花材价格只能在首店修改,并同步到分店
  279. $shop = $this->shop;
  280. $default = $shop->default ?? 0;
  281. if ($default != 1) {
  282. util::fail('请在首店修改');
  283. }
  284. $data = Yii::$app->request->post('data', ''); //data = [{id:1,inTurn:10,addPrice:2,cost:10}]
  285. $type = Yii::$app->request->post('type', '');
  286. if (empty($data) || empty($type)) {
  287. util::fail("参数错误");
  288. }
  289. if (in_array($type, ['inTurn', 'addPrice', 'cost', 'weight', 'ratio', 'ratio', 'price', 'stockWarning', 'shelfLife'])) {
  290. $data = json_decode($data, true);
  291. } else {
  292. util::fail('非法操作');
  293. }
  294. foreach ($data as $v) {
  295. if ($type == 'status') {
  296. $val = $v[$type] ?? '';
  297. if (!in_array($val, [1, 2])) {
  298. util::fail("参数错误");
  299. }
  300. }
  301. }
  302. foreach ($data as $v) {
  303. $id = $v['id'];
  304. $val = $v[$type] ?? '';
  305. //shish 20210703
  306. if ($type == 'ratio') {
  307. if (preg_match("/^[1-9][0-9]*$/", $val) == false || $val <= 0) {
  308. util::fail('单位比请填写整数,并且要大于0');
  309. }
  310. }
  311. //修改售价 shish 20210703
  312. if ($type == 'price') {
  313. $product = ProductClass::getLockById($id);
  314. if (empty($product)) {
  315. continue;
  316. }
  317. $currentItemId = $product->itemId ?? 0;
  318. if (empty($currentItemId)) {
  319. continue;
  320. }
  321. //留空恢复自动调价
  322. //所有门店同步调整
  323. if (empty($val)) {
  324. $currentPrice = bcadd($product->cost, $product->addPrice, 2);
  325. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_AUTO, 'price' => $currentPrice];
  326. } else {
  327. $upData = ['priceLabel' => ProductClass::PRICE_LABEL_CHANGE, 'price' => $val];
  328. }
  329. $shopAll = ShopClass::getAllShop($this->sjId);
  330. if (!empty($shopAll)) {
  331. foreach ($shopAll as $currentShop) {
  332. $currentShopId = $currentShop['id'] ?? 0;
  333. if (empty($currentShopId)) {
  334. continue;
  335. }
  336. ProductClass::updateByCondition(['shopId' => $currentShopId, 'itemId' => $currentItemId], $upData);
  337. }
  338. }
  339. continue;
  340. }
  341. $where = [];
  342. $where['id'] = $id;
  343. $where['shopId'] = $this->shopId;
  344. $where['sjId'] = $this->sjId;
  345. if (empty($val)) {
  346. util::fail("参数错误!");
  347. }
  348. $data = [$type => $val];
  349. ProductClass::updateByCondition($where, $data);
  350. //2021.7.6 linqh 如果是首店,则更新全部分店
  351. ProductClass::updateSyncProduct($id, $this->shopId, $data);
  352. }
  353. util::complete();
  354. }
  355. public function actionUpdate()
  356. {
  357. $post = Yii::$app->request->post();
  358. $shopAdmin = $this->shopAdmin;
  359. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  360. util::fail('超管才能修改');
  361. }
  362. $shop = $this->shop;
  363. $default = $shop->default ?? 0;
  364. if ($default != 1) {
  365. //除了上下架其它只能在首店修改
  366. if (isset($post['status']) == false) {
  367. util::fail('请在首店修改');
  368. }
  369. }
  370. $post['adminId'] = $this->adminId;
  371. ProductClass::updateProduct($post);
  372. util::complete();
  373. }
  374. //上下架状态控制 shish 20210713
  375. public function actionStatusUpdate()
  376. {
  377. $post = Yii::$app->request->post();
  378. $shopAdmin = $this->shopAdmin;
  379. $roleId = $shopAdmin['roleId'] ?? 0;
  380. $role = AdminRoleClass::getById($roleId);
  381. $roleName = $role['roleName'] ?? '';
  382. if ($roleName == '员工') {
  383. util::fail('没有权限操作哦');
  384. }
  385. $id = $post['id'] ?? 0;
  386. $status = $post['status'] ?? 1;
  387. $product = ProductClass::getById($id, true);
  388. if ($product->shopId != $this->shopId) {
  389. util::fail('没有权限操作');
  390. }
  391. $product->status = $status;
  392. $product->save();
  393. util::complete();
  394. }
  395. //花材展示状态控制 shish 20210804
  396. public function actionDelStatusUpdate()
  397. {
  398. $post = Yii::$app->request->post();
  399. $shopAdmin = $this->shopAdmin;
  400. $roleId = $shopAdmin['roleId'] ?? 0;
  401. $role = AdminRoleClass::getById($roleId);
  402. $roleName = $role['roleName'] ?? '';
  403. if ($roleName == '员工') {
  404. util::fail('没有权限操作');
  405. }
  406. $id = $post['id'] ?? 0;
  407. $delStatus = $post['delStatus'] ?? 0;
  408. $product = ProductClass::getById($id, true);
  409. if ($product->shopId != $this->shopId) {
  410. util::fail('没有权限操作');
  411. }
  412. $product->delStatus = $delStatus;
  413. $product->save();
  414. util::complete();
  415. }
  416. //详情 ruidian 2021.5.3
  417. public function actionDetail()
  418. {
  419. $id = Yii::$app->request->get('id', 0);
  420. $respond = ProductClass::getItemInfo($id);
  421. util::success($respond);
  422. }
  423. //新增花材 2021.6.21
  424. public function actionAdd()
  425. {
  426. $post = Yii::$app->request->post();
  427. $shopAdmin = $this->shopAdmin;
  428. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  429. util::fail('超管才能添加花材');
  430. }
  431. $shop = $this->shop;
  432. $default = $shop->default ?? 0;
  433. if ($default != 1) {
  434. util::fail('请在首店添加');
  435. }
  436. $post['sjId'] = $this->sjId;
  437. $post['adminId'] = $this->adminId;
  438. $post['shopId'] = $this->shopId;
  439. ProductClass::addProduct($post);
  440. util::complete('添加成功');
  441. }
  442. //获取拼音缩写 shish 20210707
  443. public function actionPy()
  444. {
  445. $name = Yii::$app->request->get('name');
  446. $py = stringUtil::py($name);
  447. util::success(['py' => $py]);
  448. }
  449. //复制花材
  450. public function actionCopy()
  451. {
  452. $shopAdmin = $this->shopAdmin;
  453. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  454. util::fail('超管才能添加花材');
  455. }
  456. $shop = $this->shop;
  457. $default = $shop->default ?? 0;
  458. if ($default != 1) {
  459. util::fail('请在首店添加');
  460. }
  461. $id = Yii::$app->request->get('id');
  462. $productData = ProductClass::getById($id);
  463. if (!$productData) {
  464. util::fail("花材不存在");
  465. }
  466. if ($productData['shopId'] != $this->shopId) {
  467. util::fail("无效花材");
  468. }
  469. //去除无用的字段
  470. unset($productData['stock']); //库存去掉
  471. unset($productData['viewNum']);
  472. unset($productData['actualSold']);
  473. unset($productData['itemId']);
  474. unset($productData['id']);
  475. unset($productData['delStatus']);
  476. $productData['copy'] = 1; //复制标识
  477. ProductClass::addProduct($productData);
  478. util::complete();
  479. }
  480. }