DistController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\custom\classes\CustomClass;
  4. use bizGhs\custom\classes\DistClass;
  5. use bizGhs\shop\classes\ShopClass;
  6. use common\components\util;
  7. use Yii;
  8. class DistController extends BaseController
  9. {
  10. //修改片区 ssh 20251117
  11. public function actionChangeDist()
  12. {
  13. $get = Yii::$app->request->get();
  14. $customId = $get['customId'] ?? 0;
  15. $id = $get['distId'] ?? 0;
  16. $custom = CustomClass::getById($customId, true);
  17. if (empty($custom)) {
  18. util::fail('没有找到客户');
  19. }
  20. if ($custom->ownMainId != $this->mainId) {
  21. util::fail('不是你的客户');
  22. }
  23. $dist = DistClass::getById($id, true);
  24. if (empty($dist)) {
  25. util::fail('没有找到片区');
  26. }
  27. if ($dist->mainId != $this->mainId) {
  28. util::fail('不是你的片区');
  29. }
  30. $custom->distId = $id;
  31. $custom->save();
  32. util::complete('修改成功');
  33. }
  34. //获取所有片区id ssh 20251117
  35. public function actionGetAllDist()
  36. {
  37. $mainId = $this->mainId;
  38. $all = DistClass::getAllByCondition(['mainId' => $mainId], null, '*');
  39. util::success(['list' => $all]);
  40. }
  41. public function actionGetList()
  42. {
  43. $where = ['mainId' => $this->mainId, 'delStatus' => 0];
  44. $list = DistClass::getDistList($where);
  45. util::success($list);
  46. }
  47. public function actionAdd()
  48. {
  49. $post = Yii::$app->request->post();
  50. $post['adminId'] = $this->adminId;
  51. $post['mainId'] = $this->mainId ?? 0;
  52. DistClass::addDist($this->shop, $post);
  53. util::complete('添加成功');
  54. }
  55. //修改分类同步 ssh 20220906
  56. public function actionUpdate()
  57. {
  58. $data = Yii::$app->request->post();
  59. $id = $data['id'] ?? 0;
  60. $name = $data['name'] ?? '';
  61. $currentDist = DistClass::getById($id, true);
  62. if (empty($currentDist)) {
  63. util::fail('没有找到片区');
  64. }
  65. if ($currentDist->mainId != $this->mainId) {
  66. util::fail('不是您的片区');
  67. }
  68. $originName = $currentDist->name ?? '';
  69. $has = DistClass::getByCondition(['mainId' => $this->mainId, 'name' => $name], true);
  70. if (!empty($has) && $id != $has->id) {
  71. util::fail('名称已经存在');
  72. }
  73. $currentDist->name = $name;
  74. $currentDist->save();
  75. //在首店修改,则父级下所有直营和加盟店,同步修改
  76. $masterShop = $this->shop;
  77. if (isset($masterShop->default) && $masterShop->default == 1 && isset($masterShop->dataSync) && $masterShop->dataSync == 1) {
  78. $sjId = $masterShop->sjId ?? 0;
  79. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  80. foreach ($shopList as $shop) {
  81. $shopId = $shop->id ?? 0;
  82. if ($shopId == $masterShop->id) {
  83. //前面已经修改过了
  84. continue;
  85. }
  86. $currentMainId = $shop->mainId ?? 0;
  87. $chainList = DistClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  88. if (!empty($chainList)) {
  89. foreach ($chainList as $chain) {
  90. if ($chain->name == $originName) {
  91. $chain->name = $name;
  92. $chain->save();
  93. }
  94. }
  95. }
  96. }
  97. }
  98. util::complete('修改成功');
  99. }
  100. //删除花材分类 ssh 202207062136
  101. public function actionDelete()
  102. {
  103. $id = Yii::$app->request->get('id', 0);
  104. $connection = Yii::$app->db;
  105. $transaction = $connection->beginTransaction();
  106. try {
  107. $myDist = DistClass::getById($id, true);
  108. if ($myDist->mainId != $this->mainId) {
  109. util::fail('不是你的片区');
  110. }
  111. $myDist->delStatus = 1;
  112. $myDist->save();
  113. $originName = $myDist->name ?? '';
  114. //在首店删除,则父级下所有直营和加盟店同步删除
  115. $masterShop = $this->shop;
  116. if (isset($masterShop->default) && $masterShop->default == 1) {
  117. $sjId = $masterShop->sjId ?? 0;
  118. $shopList = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  119. foreach ($shopList as $shop) {
  120. $shopId = $shop->id ?? 0;
  121. if ($shopId == $masterShop->id) {
  122. //前面已经删除过了
  123. continue;
  124. }
  125. $currentMainId = $shop->mainId ?? 0;
  126. $chainList = DistClass::getAllByCondition(['mainId' => $currentMainId], null, '*', null, true);
  127. if (!empty($chainList)) {
  128. foreach ($chainList as $chain) {
  129. if ($chain->name == $originName) {
  130. $chain->delStatus = 1;
  131. $chain->save();
  132. }
  133. }
  134. }
  135. }
  136. }
  137. $transaction->commit();
  138. util::complete('操作成功');
  139. } catch (\Exception $e) {
  140. $transaction->rollBack();
  141. Yii::info("失败原因:" . $e->getMessage());
  142. util::fail('删除失败');
  143. }
  144. }
  145. }