MemberWealController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace hd\controllers;
  3. use bizHd\member\classes\MemberWealClass;
  4. use bizHd\shop\classes\ShopClass;
  5. use common\components\arrayUtil;
  6. use common\components\dict;
  7. use common\components\util;
  8. use Yii;
  9. class MemberWealController extends BaseController
  10. {
  11. public $guestAccess = [];
  12. //获取充值和会员的优惠政策 ssh 20250226
  13. public function actionGetWeal()
  14. {
  15. $shop = $this->shop;
  16. $shopId = $shop->id;
  17. $weal = MemberWealClass::getAllByCondition(['shopId' => $shopId], null, '*', null);
  18. $weal = arrayUtil::arraySort($weal, 'level', SORT_ASC);
  19. if (!empty($weal)) {
  20. foreach ($weal as $key => $val) {
  21. $recharge = $val['recharge'] ?? 0;
  22. $recharge = floatval($recharge);
  23. $giv = $val['give'] ?? 0;
  24. $giv = floatval($giv);
  25. $weal[$key]['recharge'] = $recharge;
  26. $weal[$key]['give'] = $giv;
  27. }
  28. }
  29. $rechargeWeal = $shop->rechargeWeal ?? 0;
  30. util::success(['weal' => $weal, 'rechargeWeal' => $rechargeWeal]);
  31. }
  32. //获取会员的初始数据
  33. public function actionGetInitData()
  34. {
  35. $map = dict::getDict('memberLevelMap');
  36. $shop = $this->shop;
  37. $reduceDay = $shop->reduceDay ?? 30;
  38. $member = [];
  39. foreach ($map as $key => $val) {
  40. $name = $val['name'] ?? '普通会员';
  41. $levelSn = $val['levelSn'] ?? 'general';
  42. $level = $val['level'] ?? 1;
  43. $discount = $val['discount'] ?? 0.95;
  44. $recharge = $val['recharge'] ?? 0;
  45. $it = [
  46. 'name' => $name,
  47. 'levelSn' => $levelSn,
  48. 'level' => $level,
  49. 'discount' => $discount,
  50. 'recharge' => $recharge,
  51. ];
  52. $member[] = $it;
  53. }
  54. $shopId = $this->shopId;
  55. $list = MemberWealClass::getAllByCondition(['shopId' => $shopId], null, '*', null);
  56. if (!empty($list)) {
  57. $list = arrayUtil::arraySort($list, 'level', SORT_ASC);
  58. $member = [];
  59. foreach ($list as $key => $val) {
  60. $name = $val['name'] ?? '普通会员';
  61. $levelSn = $val['levelSn'] ?? 'general';
  62. $level = $val['level'] ?? 1;
  63. $discount = $val['discount'] ?? 0.95;
  64. $recharge = $val['recharge'] ?? 0;
  65. if ($discount > 1) {
  66. util::fail($name . '的折扣必须小于等于1');
  67. }
  68. if ($discount <= 0) {
  69. util::fail($name . '的折扣必须大于0');
  70. }
  71. $it = [
  72. 'name' => $name,
  73. 'levelSn' => $levelSn,
  74. 'level' => $level,
  75. 'discount' => $discount,
  76. 'recharge' => $recharge,
  77. ];
  78. $member[] = $it;
  79. }
  80. }
  81. util::success(['member' => $member, 'reduceDay' => $reduceDay]);
  82. }
  83. public function actionGetRechargeData()
  84. {
  85. $recharge = dict::getDict('memberLevelMap');
  86. $shopId = $this->shopId;
  87. $has = MemberWealClass::getByCondition(['shopId' => $shopId], true);
  88. $hasMemberDiscount = !empty($has) ? 1 : 0;
  89. $weal = MemberWealClass::getAllByCondition(['shopId' => $shopId], null, '*', 'level');
  90. if (!empty($weal)) {
  91. foreach ($recharge as $key => $val) {
  92. $level = $val['level'];
  93. $current = $weal[$level] ?? [];
  94. $newDiscount = $current['discount'] ?? 0.9;
  95. $newRecharge = $current['recharge'] ?? 0;
  96. $newRecharge = floatval($newRecharge);
  97. $newGive = $current['give'] ?? 0;
  98. $newGive = floatval($newGive);
  99. $recharge[$key]['discount'] = $newDiscount;
  100. $recharge[$key]['recharge'] = $newRecharge;
  101. $recharge[$key]['give'] = $newGive;
  102. }
  103. }
  104. $shop = $this->shop;
  105. $rechargeWeal = $shop->rechargeWeal ?? 1;
  106. $rechargeWeal = $rechargeWeal == 0 ? 1 : $rechargeWeal;
  107. util::success(['recharge' => $recharge, 'hasMemberDiscount' => $hasMemberDiscount, 'rechargeWeal' => $rechargeWeal]);
  108. }
  109. public function actionModifyRecharge()
  110. {
  111. $shop = $this->shop;
  112. $rechargeWeal = $shop->rechargeWeal ?? 0;
  113. if ($rechargeWeal != 0) {
  114. util::fail('已经设置过了,不能修改');
  115. }
  116. $post = Yii::$app->request->post();
  117. $rechargeWeal = $post['rechargeWeal'] ?? 0;
  118. $data = $post['data'] ?? [];
  119. if ($rechargeWeal == 0) {
  120. util::fail('请选择优惠类型');
  121. }
  122. if (empty($data)) {
  123. util::fail('请填写内容');
  124. }
  125. // 验证recharge和give值递增
  126. $prevRecharge = -1;
  127. $prevGive = -1;
  128. foreach ($data as $index => $item) {
  129. $currentRecharge = floatval($item['recharge'] ?? 0);
  130. $currentGive = floatval($item['give'] ?? 0);
  131. if ($currentRecharge <= $prevRecharge) {
  132. util::fail('充值金额必须递增');
  133. }
  134. if ($rechargeWeal == 2) {
  135. if ($currentGive <= $prevGive) {
  136. util::fail('赠送金额必须递增');
  137. }
  138. }
  139. $prevRecharge = $currentRecharge;
  140. $prevGive = $currentGive;
  141. }
  142. $connection = Yii::$app->db;
  143. $transaction = $connection->beginTransaction();
  144. try {
  145. $shop->rechargeWeal = $rechargeWeal;
  146. $shop->save();
  147. $params = [];
  148. foreach ($data as $key => $val) {
  149. $levelSn = $val['levelSn'];
  150. $params[$levelSn] = $val;
  151. }
  152. $shopId = $this->shopId;
  153. $list = MemberWealClass::getAllByCondition(['shopId' => $shopId], null, '*', null, true);
  154. if (empty($list)) {
  155. util::fail('请先设置会员折扣');
  156. }
  157. foreach ($list as $k => $obj) {
  158. $levelSn = $obj->levelSn ?? '';
  159. $current = $params[$levelSn] ?? [];
  160. $rechargeAmount = $current['recharge'] ?? 0;
  161. $obj->recharge = $rechargeAmount;
  162. if (!empty($current)) {
  163. $give = $current['give'] ?? 0;
  164. if ($rechargeWeal == 2) {
  165. //送钱
  166. $obj->give = $give;
  167. }
  168. $obj->save();
  169. }
  170. }
  171. $transaction->commit();
  172. util::complete('设置成功');
  173. } catch (\Exception $e) {
  174. $transaction->rollBack();
  175. Yii::info("失败原因:" . $e->getMessage());
  176. util::fail('设置失败');
  177. }
  178. }
  179. public function actionModify()
  180. {
  181. $post = Yii::$app->request->post();
  182. $shopId = $this->shopId;
  183. $mainId = $this->mainId;
  184. $has = MemberWealClass::getByCondition(['shopId' => $shopId], true);
  185. if (!empty($has)) {
  186. util::fail('已经设置过,不能再修改');
  187. }
  188. $connection = Yii::$app->db;
  189. $transaction = $connection->beginTransaction();
  190. try {
  191. $reduceDay = $post['reduceDay'] ?? 0;
  192. ShopClass::updateById($shopId, ['reduceDay' => $reduceDay]);
  193. $data = $post['data'] ?? [];
  194. if (empty($data)) {
  195. util::fail('没有折扣数据');
  196. }
  197. foreach ($data as $k => $v) {
  198. $discount = $v['discount'] ?? 0.95;
  199. $levelSn = $v['levelSn'] ?? 'general';
  200. $level = $v['level'] ?? 1;
  201. $name = $v['name'] ?? '普通会员';
  202. $recharge = $v['recharge'] ?? 0;
  203. $it = [
  204. 'mainId' => $mainId,
  205. 'shopId' => $shopId,
  206. 'level' => $level,
  207. 'levelSn' => $levelSn,
  208. 'discount' => $discount,
  209. 'name' => $name,
  210. 'recharge' => $recharge,
  211. ];
  212. MemberWealClass::add($it, true);
  213. }
  214. $transaction->commit();
  215. util::complete('设置成功');
  216. } catch (\Exception $e) {
  217. $transaction->rollBack();
  218. Yii::info("失败原因:" . $e->getMessage());
  219. util::fail('设置失败');
  220. }
  221. }
  222. }