RechargeLogic.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\recharge;
  20. use app\common\enum\PayEnum;
  21. use app\common\logic\BaseLogic;
  22. use app\common\model\RechargeOrder;
  23. use app\common\model\RechargeTemplate;
  24. use app\common\service\ConfigService;
  25. use app\common\service\FileService;
  26. /**
  27. * 充值逻辑层
  28. * Class RechargeLogic
  29. * @package app\adminapi\logic\recharge
  30. */
  31. class RechargeLogic extends BaseLogic
  32. {
  33. /**
  34. * @notes 获取充值设置
  35. * @return array
  36. * @author Tab
  37. * @date 2021/8/10 17:19
  38. */
  39. public static function getConfig()
  40. {
  41. $config = [
  42. // 充值设置
  43. 'set' => self::getSet(),
  44. // 充值规则
  45. 'rule' => self::getRule(),
  46. ];
  47. return $config;
  48. }
  49. /**
  50. * @notes 充值设置
  51. * @return array
  52. * @author Tab
  53. * @date 2021/8/10 17:31
  54. */
  55. public static function getSet()
  56. {
  57. $set = [
  58. 'open' => ConfigService::get('recharge', 'open'),
  59. 'min_amount' => ConfigService::get('recharge', 'min_amount')
  60. ];
  61. return $set;
  62. }
  63. /**
  64. * @notes 获取规则
  65. * @return array
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. * @author Tab
  70. * @date 2021/8/11 9:36
  71. */
  72. public static function getRule()
  73. {
  74. $lists = RechargeTemplate::field('id,money,award')->select()->toArray();
  75. return $lists;
  76. }
  77. /**
  78. * @notes 充值设置
  79. * @param $params
  80. * @author Tab
  81. * @date 2021/8/10 17:59
  82. */
  83. public static function setConfig($params)
  84. {
  85. try {
  86. // 更新设置
  87. self::updateSet($params);
  88. // 更新规则
  89. self::updateRule($params);
  90. return true;
  91. } catch(\Exception $e) {
  92. self::setError($e->getMessage());
  93. return false;
  94. }
  95. }
  96. /**
  97. * @notes 更新设置
  98. * @param $params
  99. * @author Tab
  100. * @date 2021/8/10 17:59
  101. */
  102. public static function updateSet($params)
  103. {
  104. if(isset($params['open'])) {
  105. ConfigService::set('recharge', 'open', $params['open']);
  106. }
  107. if(isset($params['min_amount'])) {
  108. ConfigService::set('recharge', 'min_amount', $params['min_amount']);
  109. }
  110. }
  111. /**
  112. * @notes 更新规则
  113. * @param $params
  114. * @throws \think\Exception
  115. * @author Tab
  116. * @date 2021/8/10 19:14
  117. */
  118. public static function updateRule($params)
  119. {
  120. // 未设置充值规则,直接返回
  121. if(empty($params['rule'])) {
  122. return true;
  123. }
  124. if (!is_array($params['rule'])) {
  125. throw new \Exception('充值规则格式不正确');
  126. }
  127. $data = [];
  128. foreach($params['rule'] as $key => $item) {
  129. $data[] = self::validateData($key, $item);
  130. }
  131. // 清除旧数据
  132. $deleteIds = RechargeTemplate::column('id');
  133. RechargeTemplate::destroy($deleteIds);
  134. (new RechargeTemplate())->saveAll($data);
  135. }
  136. /**
  137. * @notes 校验数据
  138. * @param $key
  139. * @param $item
  140. * @throws \think\Exception
  141. * @author Tab
  142. * @date 2021/8/10 18:58
  143. */
  144. public static function validateData($key, $item)
  145. {
  146. if(!isset($item['money'])) {
  147. throw new \think\Exception('规则' . ($key + 1) . '请输入充值金额');
  148. }
  149. if($item['money'] <= 0) {
  150. throw new \think\Exception('规则' . ($key + 1) . '充值金额须大于0');
  151. }
  152. if(!isset($item['award'])) {
  153. throw new \think\Exception('规则' . ($key + 1) . '请选择充值奖励');
  154. }
  155. if(!is_array($item['award']) || empty($item['award'])) {
  156. throw new \think\Exception('规则' . ($key + 1) . '充值奖励格式错误或为空');
  157. }
  158. foreach($item['award'] as $subItem) {
  159. if ($subItem['give_money'] < 0) {
  160. throw new \think\Exception('规则' . ($key + 1) . '充值奖励不能为负数');
  161. }
  162. }
  163. $item['award'] = json_encode($item['award'], JSON_UNESCAPED_UNICODE);
  164. return $item;
  165. }
  166. /**
  167. * @notes 充值数据中心
  168. * @return array
  169. * @author Tab
  170. * @date 2021/8/11 16:45
  171. */
  172. public static function dataCenter()
  173. {
  174. return [
  175. 'recharge_data' => self::rechargeData(),
  176. 'top_user' => self::topUser(),
  177. 'top_rule' => self::topRule(),
  178. ];
  179. }
  180. /**
  181. * @notes 充值数据
  182. * @return array
  183. * @author Tab
  184. * @date 2021/8/11 16:48
  185. */
  186. public static function rechargeData()
  187. {
  188. $totalAmount = RechargeOrder::where(['pay_status' => PayEnum::ISPAID
  189. ])->sum('order_amount');
  190. $totalTimes = RechargeOrder::where(['pay_status' => PayEnum::ISPAID
  191. ])->count();
  192. return [
  193. 'total_amount' => $totalAmount,
  194. 'total_times' => $totalTimes
  195. ];
  196. }
  197. /**
  198. * @notes 用户充值榜
  199. * @return mixed
  200. * @author Tab
  201. * @date 2021/8/11 16:55
  202. */
  203. public static function topUser()
  204. {
  205. $field = 'u.nickname,u.avatar,sum(ro.order_amount) as total_amount';
  206. $lists = RechargeOrder::alias('ro')
  207. ->leftJoin('user u', 'u.id = ro.user_id')
  208. ->field($field)
  209. ->where('pay_status', PayEnum::ISPAID)
  210. ->group('ro.user_id')
  211. ->order('total_amount', 'desc')
  212. ->limit(10)
  213. ->select()
  214. ->toArray();
  215. foreach($lists as &$item) {
  216. $item['avatar'] = FileService::getFileUrl( $item['avatar']);
  217. }
  218. return $lists;
  219. }
  220. /**
  221. * @notes 充值规则榜
  222. * @return mixed
  223. * @author Tab
  224. * @date 2021/8/11 17:03
  225. */
  226. public static function topRule()
  227. {
  228. $field = 'rt.money, count(ro.id) as total_num';
  229. $lists =RechargeTemplate::alias('rt')
  230. ->leftJoin('recharge_order ro', 'ro.template_id = rt.id')
  231. ->field($field)
  232. ->where('pay_status', PayEnum::ISPAID)
  233. ->group('rt.id')
  234. ->order('total_num', 'desc')
  235. ->limit(10)
  236. ->select()
  237. ->toArray();
  238. return $lists;
  239. }
  240. }