ItemClassController.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\item\classes\ItemClassClass;
  4. use bizGhs\item\services\ItemClassService;
  5. use bizGhs\product\classes\ProductClass;
  6. use bizGhs\shop\classes\ShopClass;
  7. use common\components\imgUtil;
  8. use common\components\util;
  9. use Yii;
  10. class ItemClassController extends BaseController
  11. {
  12. //获取花材列表的菜单 ssh 20240222
  13. public function actionGetMenu()
  14. {
  15. $mainId = $this->mainId;
  16. $itemList = ProductClass::getAllByCondition(['mainId' => $mainId, 'delStatus' => 0, 'status' => 1], null, 'id,name,reachNum,reachNumDiscount,presell,discountPrice', null, true);
  17. $arr = [];
  18. if (!empty($itemList)) {
  19. foreach ($itemList as $item) {
  20. $reachNum = $item->reachNum ?? 0;
  21. $reachNumDiscount = $item->reachNumDiscount ?? 0;
  22. if (isset($item->presell) && $item->presell == 1) {
  23. $arr['ys'] = [
  24. 'id' => -3,
  25. 'name' => '预售专区',
  26. 'shortCover' => 'item_class/mrfl.png',
  27. 'cover' => imgUtil::groupImg('item_class/mrfl.png') . "?x-oss-process=image/resize,m_fill,h_130,w_130"
  28. ];
  29. }
  30. if (isset($item->discountPrice) && $item->discountPrice > 0) {
  31. $arr['tj'] = [
  32. 'id' => -1,
  33. 'name' => '今日特价',
  34. 'shortCover' => 'item_class/tj.jpg',
  35. 'cover' => imgUtil::groupImg('item_class/tj.jpg') . "?x-oss-process=image/resize,m_fill,h_130,w_130"
  36. ];
  37. }
  38. if ($reachNum > 0 && $reachNumDiscount > 0) {
  39. $arr['mj'] = [
  40. 'id' => -2,
  41. 'name' => '今日满减',
  42. 'shortCover' => 'item_class/mrfl.jpg',
  43. 'cover' => imgUtil::groupImg('item_class/mrfl.jpg') . "?x-oss-process=image/resize,m_fill,h_130,w_130"
  44. ];
  45. }
  46. }
  47. }
  48. $classInfo = ItemClassClass::getGhsItemClassAll($mainId);
  49. if (!empty($arr)) {
  50. $arr = array_values($arr);
  51. $classInfo = array_merge($arr, $classInfo);
  52. }
  53. util::success(['class' => $classInfo]);
  54. }
  55. //采购选分类有变化通知
  56. public function actionGetClassRemind()
  57. {
  58. $cacheKey = 'ghs_class_change_remind_' . $this->shopAdminId;
  59. $has = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  60. $hasStatus = !empty($has) ? 1 : 0;
  61. util::success(['hasRemind' => $hasStatus]);
  62. }
  63. //采购选分类有变化通知
  64. public function actionHasClassRemind()
  65. {
  66. $cacheKey = 'ghs_class_change_remind_' . $this->shopAdminId;
  67. Yii::$app->redis->executeCommand('SET', [$cacheKey, 'has']);
  68. util::complete();
  69. }
  70. public function actionList()
  71. {
  72. $where = ['mainId' => $this->mainId];
  73. $list = ItemClassClass::getGhsItemClassList($where);
  74. util::success($list);
  75. }
  76. //花材列表页的分类,包含今日特价分类 ssh 20220315
  77. public function actionShowClass()
  78. {
  79. $list = ItemClassClass::getShopClass($this->mainId);
  80. util::success($list);
  81. }
  82. public function actionGetAllClass()
  83. {
  84. //中央ID
  85. $list = ItemClassClass::getGhsItemClassAll($this->mainId);
  86. util::success(['list' => $list]);
  87. }
  88. public function actionAdd()
  89. {
  90. $post = Yii::$app->request->post();
  91. $post['adminId'] = $this->adminId;
  92. $post['mainId'] = $this->mainId ?? 0;
  93. ItemClassClass::addGhsItemClass($this->shop, $post);
  94. util::complete('添加成功');
  95. }
  96. //修改分类同步 ssh 20220906
  97. public function actionUpdate()
  98. {
  99. $data = Yii::$app->request->post();
  100. $id = $data['id'] ?? 0;
  101. $name = $data['name'] ?? '';
  102. $inTurn = $data['inTurn'] ?? 0;
  103. $print = $data['print'] ?? 1;
  104. $class = ItemClassClass::getById($id, true);
  105. if (empty($class)) {
  106. util::fail('没有找到分类');
  107. }
  108. if ($class->mainId != $this->mainId) {
  109. util::fail('不是您的分类');
  110. }
  111. $originName = $class->name ?? '';
  112. $has = ItemClassClass::getByCondition(['mainId' => $this->mainId, 'name' => $name], true);
  113. if (!empty($has) && $id != $has->id) {
  114. util::fail('名称已经存在');
  115. }
  116. if ($class->isDefault != 1) {
  117. $class->name = $name;
  118. }
  119. $class->inTurn = $inTurn;
  120. $class->print = $print;
  121. $class->save();
  122. //在首店修改,则父级下所有直营和加盟店,同步修改
  123. $masterShop = $this->shop;
  124. if (isset($masterShop->default) && $masterShop->default == 1 && isset($masterShop->dataSync) && $masterShop->dataSync == 1) {
  125. $sjId = $masterShop->sjId ?? 0;
  126. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  127. foreach ($shopList as $shop) {
  128. $shopId = $shop->id ?? 0;
  129. if ($shopId == $masterShop->id) {
  130. //前面已经修改过了
  131. continue;
  132. }
  133. $currentMainId = $shop->mainId ?? 0;
  134. $chainList = ItemClassClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  135. if (!empty($chainList)) {
  136. foreach ($chainList as $chain) {
  137. if ($chain->name == $originName) {
  138. if ($chain->isDefault != 1) {
  139. $chain->name = $name;
  140. }
  141. $chain->inTurn = $inTurn;
  142. $chain->print = $print;
  143. $chain->save();
  144. }
  145. }
  146. }
  147. }
  148. }
  149. util::complete('修改成功');
  150. }
  151. //删除花材分类 ssh 202207062136
  152. public function actionDelete()
  153. {
  154. $id = Yii::$app->request->get('id', 0);
  155. $connection = Yii::$app->db;
  156. $transaction = $connection->beginTransaction();
  157. try {
  158. $default = ItemClassClass::getByCondition(['mainId' => $this->mainId, 'isDefault' => 1], true);
  159. $defaultClassId = $default->id ?? 0;
  160. if (empty($defaultClassId)) {
  161. util::fail('没有默认分类');
  162. }
  163. if ($defaultClassId == $id) {
  164. util::fail('不能删除默认分类');
  165. }
  166. $class = ItemClassClass::getById($id, true);
  167. ItemClassClass::valid($class, $this->mainId);
  168. $originName = $class->name ?? '';
  169. $class->delete();
  170. ProductClass::updateByCondition(['classId' => $id], ['classId' => $defaultClassId]);
  171. //在首店删除,则父级下所有直营和加盟店同步删除
  172. $masterShop = $this->shop;
  173. if (isset($masterShop->default) && $masterShop->default == 1) {
  174. $sjId = $masterShop->sjId ?? 0;
  175. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  176. foreach ($shopList as $shop) {
  177. $shopId = $shop->id ?? 0;
  178. if ($shopId == $masterShop->id) {
  179. //前面已经删除过了
  180. continue;
  181. }
  182. $currentMainId = $shop->mainId ?? 0;
  183. $chainList = ItemClassClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  184. if (!empty($chainList)) {
  185. foreach ($chainList as $chain) {
  186. if ($chain->name == $originName) {
  187. $currentId = $chain->id;
  188. $chain->delete();
  189. $default = ItemClassClass::getByCondition(['mainId' => $currentMainId, 'isDefault' => 1], true);
  190. $defaultClassId = $default->id ?? 0;
  191. if (empty($defaultClassId)) {
  192. util::fail('分店同步删除时,找到默认分类');
  193. }
  194. ProductClass::updateByCondition(['classId' => $currentId], ['classId' => $defaultClassId]);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. $transaction->commit();
  201. util::complete('操作成功');
  202. } catch (\Exception $e) {
  203. $transaction->rollBack();
  204. Yii::info("失败原因:" . $e->getMessage());
  205. util::fail('删除失败');
  206. }
  207. }
  208. //开单: 获取所有分类
  209. public function actionAll()
  210. {
  211. //中央ID
  212. $data = ItemClassClass::getGhsItemClassAll($this->mainId);
  213. util::success($data);
  214. }
  215. //修改排序 ssh 20210701
  216. public function actionChangeInTurn()
  217. {
  218. $sortData = Yii::$app->request->post('sortData');
  219. if (empty($sortData)) {
  220. util::fail('请修改你要排序的分类');
  221. }
  222. $sortArr = json_decode($sortData, true);
  223. if (empty($sortArr) || is_array($sortArr) == false) {
  224. util::fail('请修改分类');
  225. }
  226. foreach ($sortArr as $item) {
  227. $id = $item['id'];
  228. $inTurn = $item['inTurn'];
  229. if (is_numeric($inTurn) == false || intval($inTurn) != $inTurn) {
  230. continue;
  231. }
  232. ItemClassClass::updateById($id, ['inTurn' => $inTurn]);
  233. }
  234. util::complete();
  235. }
  236. }