StoreWeightController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace addons\Happiness\backend\controllers;
  3. use addons\Happiness\common\enums\HappinessEnum;
  4. use addons\Happiness\common\logic\weight\PoolLogLogic;
  5. use addons\Happiness\common\models\HappinessWeightPoolJob;
  6. use addons\Happiness\common\models\HappinessWeightPoolOrderLog;
  7. use addons\Happiness\common\models\HappinessWeightPoolLog;
  8. use Yii;
  9. use common\enums\StatusEnum;
  10. use common\models\user\User;
  11. use common\models\user\UserLevel;
  12. use Exception;
  13. /**
  14. * 分红控制器
  15. *
  16. * Class PoolController
  17. * @package addons\Happiness\backend\controllers
  18. */
  19. class StoreWeightController extends BaseController
  20. {
  21. /**
  22. * 首页 分红池首页
  23. *
  24. * @return string
  25. */
  26. public function actionIndex()
  27. {
  28. if (Yii::$app->request->isAjax) {
  29. if (Yii::$app->request->isGet) {
  30. $get = Yii::$app->request->get();
  31. $where = $list = [];
  32. $where[] = ['=', 'status', StatusEnum::ENABLED];
  33. $where[] = ['=', 'mall_id', $this->mall_id];
  34. if (!empty($get['order_no'])) {
  35. $where[] = ['order_no'=>$get['order_no']];
  36. }
  37. if (!empty($get['user_id'])) {
  38. $where[] = ['user_id'=>$get['user_id']];
  39. }
  40. if (!empty($get['date'])) {
  41. $where[]=['between', 'created_at', strtotime($get[0]), strtotime($get[1])];
  42. }
  43. if (!empty($get['change_type'])) {
  44. if($get['change_type']=='add')
  45. {
  46. $where[] = ['>','amount',0];
  47. }
  48. else{
  49. $where[] = ['<','amount',0];
  50. }
  51. }
  52. if (isset($get['keyword']) && !empty($get['keyword'])) {
  53. $user_list = User::lists([
  54. 'where' => [
  55. ['mall_id' => $this->mall_id],
  56. ['or', ['like', 'nickname', $get['keyword']], ['=', 'mobile', $get['keyword']]]
  57. ],
  58. 'select' => 'id'
  59. ]);
  60. $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
  61. }
  62. $with = [
  63. 'user' => function ($query) {
  64. return $query->select('id, nickname, avatar_url, mobile');
  65. }
  66. ];
  67. $order = 'created_at desc';
  68. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15,'with'=>$with);
  69. $list = HappinessWeightPoolOrderLog::listPage($params, false, true);
  70. if ($list === false) return $this->error(HappinessWeightPoolOrderLog::getStaticError());
  71. foreach ($list['list'] as &$val) {
  72. $date_limit = explode('-', $val['date_limit']);
  73. $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
  74. }
  75. unset($val);
  76. $total_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id,'status'=>1])->sum('amount');//实际已结算的订单分红总额
  77. $total_settle_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id,'settlement_status' => 1,'status'=>1])->sum('amount');//实际已结算的订单分红总额
  78. $total_send_amount=HappinessWeightPoolLog::find()->where(['mall_id' => $this->mall_id,'status'=>1])->sum('amount');//实际用户获得的分红
  79. $now_amount=HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id, 'settlement_status' => 0,'status'=>1])->sum('amount');//当前未结算的分红,就是分红池的额度
  80. $list['orderStatistics'] = [
  81. 'total_amount' =>$total_amount??0,
  82. 'total_settle_amount' => $total_settle_amount??0,
  83. 'total_send_amount' => $total_send_amount??0,
  84. 'now_amount' => $now_amount??0
  85. ];
  86. return $this->success('获取成功', $list);
  87. }
  88. }
  89. return $this->render($this->action->id);
  90. }
  91. public function actionRecharge()
  92. {
  93. if (Yii::$app->request->isAjax) {
  94. if (Yii::$app->request->isPost) {
  95. $params = Yii::$app->request->post();
  96. $rules = [
  97. [['price'], 'required'],
  98. [['price'], 'number'],
  99. [['remark'], 'string'],
  100. ];
  101. $res = Yii::$app->services->validate->validate($params, $rules);
  102. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  103. try {
  104. $params['remark'] = !empty($params['remark']) ? trim($params['remark']) : '管理员调整,' . ($params['price'] > 0 ? '增加' : '减少') . strval(abs($params['price']));
  105. $before_amount = HappinessWeightPoolOrderLog::find()->where(['mall_id' => $this->mall_id, 'settlement_status' => 0])->sum('amount')??0;
  106. if(($before_amount+$params['price'])<0)
  107. {
  108. throw new Exception('奖励池金额不足'.abs($params['price']));
  109. }
  110. if (HappinessWeightPoolOrderLog::addData([
  111. 'mall_id' => $this->mall_id,
  112. 'date' => date('YmdHis'),
  113. 'amount' => $params['price'],
  114. 'user_id'=>0,
  115. 'price'=>0,
  116. 'pay_price'=>0,
  117. 'order_id' => 0,
  118. 'order_no' => '',
  119. 'before_amount' => $before_amount,
  120. 'after_amount' => bcadd($before_amount, $params['price'], 2),
  121. 'desc'=>$params['remark']
  122. ]) === false) {
  123. throw new \Exception(HappinessWeightPoolOrderLog::getStaticError());
  124. }
  125. return $this->success('调整成功');
  126. } catch (Exception $e) {
  127. return $this->error($e->getMessage());
  128. }
  129. }
  130. return $this->error('非法的请求');
  131. }
  132. }
  133. /**
  134. * 首页 结算周期
  135. *
  136. * @return string
  137. */
  138. public function actionList()
  139. {
  140. if (Yii::$app->request->isAjax) {
  141. if (Yii::$app->request->isGet) {
  142. $get = Yii::$app->request->get();
  143. $where = $list = [];
  144. $where[] = ['=', 'status', StatusEnum::ENABLED];
  145. $where[] = ['=', 'mall_id', $this->mall_id];
  146. if (isset($get['start_at']) && !empty($get['start_at'])) {
  147. $where[] = ['>=', 'created_at', strtotime($get['start_at'])];
  148. }
  149. if (isset($get['end_at']) && !empty($get['end_at'])) {
  150. $where[] = ['<=', 'created_at', strtotime($get['end_at'])];
  151. }
  152. $order = 'created_at desc';
  153. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15);
  154. $list = HappinessWeightPoolJob::listPage($params, false, true);
  155. if ($list === false) return $this->error(HappinessWeightPoolJob::getStaticError());
  156. foreach ($list['list'] as &$val) {
  157. $date_limit = explode('-', $val['date_limit']);
  158. $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
  159. }
  160. unset($val);
  161. return $this->success('获取成功', compact('list'));
  162. }
  163. }
  164. return $this->render($this->action->id);
  165. }
  166. /**
  167. * 结算明细
  168. */
  169. public function actionDetail()
  170. {
  171. if (Yii::$app->request->isAjax) {
  172. $get = Yii::$app->request->get();
  173. $where = $list = [];
  174. $where[] = ['=', 'status', StatusEnum::ENABLED];
  175. $where[] = ['=', 'mall_id', $this->mall_id];
  176. $where[] = ['=', 'job_id', $get['job_id']];
  177. if (isset($get['user_id']) && !empty($get['user_id'])) {
  178. $where[] = ['user_id' => $get['user_id']];
  179. }
  180. $user_where= [];
  181. if(isset($get['level'])&& !empty($get['level']))
  182. {
  183. $user_where[]= ['level' => $get['level']];
  184. }
  185. if (isset($get['keyword']) && !empty($get['keyword'])) {
  186. $user_where[]=[
  187. 'or', ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']]];
  188. }
  189. if (!empty($user_where)) {
  190. $user_where=array_merge(['mall_id' => $this->mall_id],$user_where);
  191. $user_list = User::lists([
  192. 'where' => $user_where,
  193. 'select' => 'id'
  194. ]);
  195. $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
  196. }
  197. $with = [
  198. 'user' => function ($query) {
  199. return $query->select('id, nickname, avatar_url, mobile');
  200. }
  201. ];
  202. $order = 'created_at desc';
  203. $params = array('where' => $where, 'order' => $order, 'with' => $with, 'limit' => $get['limit'] ?? 15);
  204. $list = HappinessWeightPoolLog::listPage($params, false, true);
  205. $levels =UserLevel::getListIndexByLevelColumn($this->mall_id);
  206. if ($list === false) return $this->error(HappinessWeightPoolLog::getStaticError());
  207. return $this->success('获取成功', compact('list','levels'));
  208. }
  209. return $this->render($this->action->id);
  210. }
  211. //手动执行
  212. public function actionDoRun()
  213. {
  214. try {
  215. $logic = new PoolLogLogic();
  216. $res = $logic->executeNoAuto($this->mall_id);
  217. if($res == true) {
  218. return $this->success('已经执行');
  219. }
  220. } catch (\Exception $e) {
  221. return $this->error($e->getMessage());
  222. }
  223. }
  224. public function actionOrderList()
  225. {
  226. if (Yii::$app->request->isAjax) {
  227. if (Yii::$app->request->isGet) {
  228. $get = Yii::$app->request->get();
  229. $where = $list = [];
  230. $where[] = ['=', 'status', StatusEnum::ENABLED];
  231. $where[] = ['=', 'mall_id', $this->mall_id];
  232. if (!empty($get['order_no'])) {
  233. $where[] = ['order_no'=>$get['order_no']];
  234. }
  235. if (!empty($get['user_id'])) {
  236. $where[] = ['user_id'=>$get['user_id']];
  237. }
  238. if (!empty($get['date'])) {
  239. $where[]=['between', 'created_at', strtotime($get[0]), strtotime($get[1])];
  240. }
  241. if (isset($get['keyword']) && !empty($get['keyword'])) {
  242. $user_list = User::lists([
  243. 'where' => [
  244. ['mall_id' => $this->mall_id],
  245. ['or', ['like', 'nickname', $get['keyword']], ['like', 'mobile', $get['keyword']]],
  246. ],
  247. 'select' => 'id'
  248. ]);
  249. $where[] = ['user_id' => array_column($user_list, 'id') ?? []];
  250. }
  251. $with = [
  252. 'user' => function ($query) {
  253. return $query->select('id, nickname, avatar_url, mobile');
  254. }
  255. ];
  256. $order = 'created_at desc';
  257. $params = array('where' => $where, 'order' => $order, 'limit' => $get['limit'] ?? 15,'with'=>$with);
  258. $list = HappinessWeightPoolOrderLog::listPage($params, false, true);
  259. if ($list === false) return $this->error(HappinessWeightPoolOrderLog::getStaticError());
  260. foreach ($list['list'] as &$val) {
  261. $date_limit = explode('-', $val['date_limit']);
  262. $val['date_limit'] = date('Y-m-d', strtotime($date_limit[0])) . '至' . date('Y-m-d', strtotime($date_limit[1]));
  263. }
  264. return $this->success('获取成功', $list);
  265. }
  266. }
  267. return $this->render($this->action->id);
  268. }
  269. }