DistController.php 5.7 KB

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