Reward.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace app\admin\controller\shopro\commission;
  3. use app\admin\controller\shopro\Base;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. use Exception;
  8. /**
  9. * 佣金明细
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Reward extends Base
  14. {
  15. /**
  16. * Order模型对象
  17. * @var \app\admin\model\shopro\commission\Reward
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\shopro\commission\Reward;
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //设置过滤方法
  36. $this->request->filter(['strip_tags']);
  37. if ($this->request->isAjax()) {
  38. $nobuildfields = [
  39. 'buyer_nickname','buyer_mobile',
  40. 'agent_nickname','agent_mobile',
  41. 'order_sn',
  42. ];
  43. list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
  44. $total = $this->buildSearch()
  45. ->where($where)
  46. ->order($sort, $order)
  47. ->count();
  48. // 查询分页数据
  49. $list = $this->buildSearch()
  50. ->with(['order' => function ($query) {
  51. $query->removeOption('soft_delete')->field('id,type,order_sn,user_id,activity_type,goods_amount,status,total_amount,score_amount,total_fee,pay_fee,score_fee,pay_type,paytime,platform,ext,createtime,updatetime');
  52. }, 'buyer', 'agent'])
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->limit($offset, $limit)
  56. ->select();
  57. // 处理顶部统计数据
  58. $commission_total = 0;
  59. $commissioned_total = 0;
  60. $delay_total = 0;
  61. $commission_back_total = 0;
  62. $total_page = intval(ceil($total / 1000));
  63. for ($i = 0; $i < $total_page; $i++) {
  64. $totalList = $this->buildSearch()
  65. ->where($where)
  66. ->order($sort, $order)
  67. ->limit(($i * 1000), 1000)
  68. ->select();
  69. foreach ($totalList as $key => $reward) {
  70. if ($reward['status'] == 1) {
  71. $commissioned_total += $reward['commission'];
  72. } else if ($reward['status'] == -1) {
  73. $commission_back_total += $reward['commission'];
  74. } else if ($reward['status'] == 0) {
  75. $delay_total += $reward['commission'];
  76. }
  77. $commission_total += $reward['commission'];
  78. }
  79. }
  80. $result = [
  81. "total" => $total,
  82. "rows" => $list,
  83. "commission_total" => number_format($commission_total, 2, '.', ''),
  84. "commissioned_total" => number_format($commissioned_total, 2, '.', ''),
  85. "delay_total" => number_format($delay_total, 2, '.', ''),
  86. "commission_back_total" => number_format($commission_back_total, 2, '.', '')
  87. ];
  88. $this->success('佣金明细', null, $result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 修改佣金金额
  94. *
  95. * @param int $id
  96. * @return void
  97. */
  98. public function edit ($id = null) {
  99. $params = $this->request->post();
  100. if (!$id) {
  101. $id = $this->request->get('id');
  102. }
  103. $row = $this->model->get($id);
  104. if (!$row) {
  105. $this->error(__('No Results were found'));
  106. }
  107. if ($row['status'] != 0) {
  108. $this->error('当前状态不可修改');
  109. }
  110. $row->commission = $params['commission'];
  111. $row->save();
  112. $this->success('修改成功', null, $row);
  113. }
  114. /**
  115. * 分佣
  116. *
  117. * @param int $id
  118. * @return void
  119. */
  120. public function runCommission($id = null)
  121. {
  122. if (!$id) {
  123. $id = $this->request->get('id');
  124. }
  125. $row = $this->model->get($id);
  126. $result = \think\Db::transaction(function () use ($row) {
  127. return (new \addons\shopro\library\commission\Reward)->runCommissionReward('admin', $row);
  128. });
  129. if ($result) {
  130. $this->success('结算成功');
  131. } else {
  132. $this->error('佣金已结算,请不要重复结算');
  133. }
  134. }
  135. /**
  136. * 佣金退回
  137. *
  138. * @param int $id
  139. * @return void
  140. */
  141. public function backCommission($id = null)
  142. {
  143. if (!$id) {
  144. $id = $this->request->get('id');
  145. }
  146. $row = $this->model->get($id);
  147. $result = \think\Db::transaction(function () use ($row) {
  148. return (new \addons\shopro\library\commission\Reward)->backCommissionReward('admin', $row);
  149. });
  150. if ($result) {
  151. $this->success('退回成功');
  152. } else {
  153. $this->error('佣金已退回或未结算');
  154. }
  155. }
  156. /**
  157. * 取消佣金
  158. *
  159. * @param int $id
  160. * @return void
  161. */
  162. public function cancelCommission($id = null)
  163. {
  164. if (!$id) {
  165. $id = $this->request->get('id');
  166. }
  167. $row = $this->model->get($id);
  168. $result = \think\Db::transaction(function () use ($row) {
  169. return (new \addons\shopro\library\commission\Reward)->cancelCommissionReward('admin', $row);
  170. });
  171. if ($result) {
  172. $this->success('退回成功');
  173. } else {
  174. $this->error('佣金已退回或未结算');
  175. }
  176. }
  177. private function buildSearch()
  178. {
  179. $filter = $this->request->get("filter", '');
  180. $filter = (array)json_decode($filter, true);
  181. $filter = $filter ? $filter : [];
  182. // 用户'parent_nickname','parent_mobile',
  183. // 购买人
  184. $buyer_nickname = isset($filter['buyer_nickname']) ? $filter['buyer_nickname'] : '';
  185. $buyer_mobile = isset($filter['buyer_mobile']) ? $filter['buyer_mobile'] : '';
  186. // 分销用户
  187. $agent_nickname = isset($filter['agent_nickname']) ? $filter['agent_nickname'] : '';
  188. $agent_mobile = isset($filter['agent_mobile']) ? $filter['agent_mobile'] : '';
  189. // 订单 & 商品
  190. $order_sn = isset($filter['order_sn']) ? $filter['order_sn'] : '';
  191. // 当前表名
  192. $tableName = $this->model->getQuery()->getTable();
  193. $orders = $this->model;
  194. // 购买人查询
  195. if ($buyer_nickname || $buyer_mobile) {
  196. $orders = $orders->whereExists(function ($query) use ($buyer_nickname, $buyer_mobile, $tableName) {
  197. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  198. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.buyer_id');
  199. if ($buyer_nickname) {
  200. $query = $query->where('nickname', 'like', "%{$buyer_nickname}%");
  201. }
  202. if ($buyer_mobile) {
  203. $query = $query->where('mobile', 'like', "%{$buyer_mobile}%");
  204. }
  205. return $query;
  206. });
  207. }
  208. // 分销用户昵称,手机号
  209. if ($agent_nickname || $agent_mobile) {
  210. $orders = $orders->whereExists(function ($query) use ($agent_nickname, $agent_mobile, $tableName) {
  211. $userTableName = (new \app\admin\model\User())->getQuery()->getTable();
  212. $query = $query->table($userTableName)->where($userTableName . '.id=' . $tableName . '.agent_id');
  213. if ($agent_nickname) {
  214. $query = $query->where('nickname', 'like', "%{$agent_nickname}%");
  215. }
  216. if ($agent_mobile) {
  217. $query = $query->where('mobile', 'like', "%{$agent_mobile}%");
  218. }
  219. return $query;
  220. });
  221. }
  222. // 订单号查询
  223. if ($order_sn) {
  224. $orders = $orders->whereExists(function ($query) use ($order_sn, $tableName) {
  225. $orderTableName = (new \app\admin\model\shopro\order\Order())->getQuery()->getTable();
  226. $query = $query->table($orderTableName)->where($orderTableName . '.id=' . $tableName . '.order_id');
  227. if ($order_sn) {
  228. $query = $query->where('order_sn', 'like', "%{$order_sn}%");
  229. }
  230. return $query;
  231. });
  232. }
  233. return $orders;
  234. }
  235. }