Order.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Config;
  5. use app\common\model\Goods;
  6. use app\common\model\MoneyLog;
  7. use app\common\model\Order AS OrderModel;
  8. use app\common\model\Users;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 首页接口
  13. */
  14. class Order extends Api
  15. {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 订单列表
  20. * @return void
  21. * @throws \think\exception\DbException
  22. */
  23. public function base()
  24. {
  25. $user = $this->auth->getUser();
  26. $data['banner_list'] = 'https://dapp-static.oss-cn-shenzhen.aliyuncs.com/jue-jin-lu/3.pnggS02ouwrJfiF65979ec74db73';
  27. $data['day_tasks_num'] = (new Config())->getValue('day_tasks_num');//每日任务数
  28. $data['task_income'] = (new Config())->getValue('task_income');//单次任务收益
  29. $data['task_num'] = $user['task_num'];//今日已做任务
  30. $data['balance'] = $user['freeze'] < 0 ? $user['freeze']: $user['balance'];
  31. $data['bonus_sum'] = $user['bonus_sum'];
  32. $data['bonus_today'] = (new OrderModel())
  33. ->where('user_id', $user['id'])
  34. ->where('status', OrderModel::Success)
  35. ->whereTime('create_time', '>=', strtotime('today'))
  36. ->sum('bonus');
  37. $this->success('', $data);
  38. }
  39. /**
  40. * 订单列表
  41. * @return void
  42. * @throws \think\exception\DbException
  43. */
  44. public function list()
  45. {
  46. $user = $this->auth->getUser();
  47. $paginator = OrderModel::where('user_id', $user['id'])
  48. ->field('order_no,amount,bonus,status,create_time')
  49. ->order('id DESC')
  50. ->paginate($this->pageSize);
  51. foreach ($paginator as $k => $v) {
  52. $status = $v['status'] ==0 ?100: $v['status'];
  53. $paginator[$k]['status_name'] = (new OrderModel())->getStatusNames($status);
  54. }
  55. $res_data = $this->buildResp($paginator->total(), $paginator->currentPage(), $paginator->items());
  56. $res_data['bonus_sum'] = $user['bonus_sum'];
  57. $this->success('', $res_data);
  58. }
  59. /**
  60. * 获取订单
  61. * @return void
  62. * @throws \think\exception\DbException
  63. */
  64. public function get()
  65. {
  66. $user = $this->auth->getUser();
  67. if($user['open_task'] != 1){
  68. $this->error(__('暂停抢单'));
  69. }
  70. if(bccomp($user['freeze'], 0, 2) ===-1){
  71. //冻结金额< 0时,需要充值
  72. $this->error(__('余额不足'));
  73. }
  74. if(!($user['balance'] > 0)){
  75. $this->error(__('余额不足'));
  76. }
  77. $day_tasks_num = (new Config())->getValue('day_tasks_num');//单日任务数
  78. $task_income = (new Config())->getValue('task_income');//单次收益
  79. $amount_mini = $user['balance'] * 0.4;
  80. $amount_max = $user['balance'] * 0.8;
  81. if($user['task_num'] >= $day_tasks_num){
  82. if(!empty($user['task_last_time']) && (date('md',time()) != date('md', $user['task_last_time']))){
  83. //当日接单量 >= 任务数时,最后一次接单时间和当前不是同一天,则重置当日接单量
  84. $user['task_num'] = 0;
  85. Users::where('id', $user['id'])->update([
  86. 'task_num' => 0
  87. ]);
  88. }else{
  89. $this->error(__('今日任务已完成'));
  90. }
  91. }
  92. $check_order = (new OrderModel)
  93. ->where('user_id', $user['id'])
  94. ->where('status', '<', OrderModel::Success)
  95. ->count();
  96. if($check_order){
  97. $this->error(__('有未完成订单'));
  98. }
  99. $freeze = false; //是否有冻结金额
  100. if($user['is_limit_task'] == 1){
  101. //卡单 limit_task 字段值为json {"which_start":"7","min_amount":"100","max_amount":"1000","income_multiple":"2"}
  102. $limit_task = json_decode($user['limit_task'], true);
  103. if(empty($limit_task)){
  104. $this->error(__('参数错误'));
  105. }
  106. $val_arr = array_column($limit_task, 'which_start');
  107. $key = array_search($user['task_num'] + 1, $val_arr);
  108. if($key !== false){
  109. //从这单开始卡单
  110. if(!($user['freeze'] < 0)){
  111. //冻结金额不小于0时,说明未卡单,则进行卡单操作:向用户展示余额时,显示 负的订单金额
  112. $freeze = true;
  113. }
  114. $task_income = $task_income * $limit_task[$key]['income_multiple'];//单次收益
  115. $amount_mini = $limit_task[$key]['min_amount'];
  116. $amount_max = $limit_task[$key]['max_amount'];
  117. }
  118. }
  119. $goods_info = (new Goods())
  120. ->fetchSql(false)
  121. ->whereBetween('price', [$amount_mini, $amount_max])
  122. ->where('status', 1)
  123. ->orderRaw("RAND()")
  124. ->find();
  125. if(empty($goods_info)){
  126. $this->error(__('未匹配到商品'));
  127. }
  128. $order_data = [
  129. 'order_no' => 'O' . $user['id'] . time(),
  130. 'user_id' => $user['id'],
  131. //'title' => $goods_info['title'],
  132. 'amount' => $goods_info['price'],
  133. 'bonus' => $goods_info['price'] * $task_income,
  134. 'user_type' => $user['user_type'],
  135. 'status' => OrderModel::Pending,
  136. ];
  137. // 启动事务
  138. Db::startTrans();
  139. try {
  140. if($freeze){
  141. //卡单操作,不扣款,只冻结订单金额
  142. $freeze = Users::where('id', $user['id'])->update(['freeze' => -$goods_info['price']]);
  143. $order_data['status'] = OrderModel::Default;
  144. }else{
  145. //正常订单 账变
  146. (new MoneyLog())->change($user['id'], -$goods_info['price'], MoneyLog::Pay, '', '');
  147. }
  148. // 创建订单
  149. OrderModel::create($order_data);
  150. // 提交事务
  151. Db::commit();
  152. } catch (Exception $e) {
  153. // 回滚事务
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. }
  157. unset($order_data['user_type']);
  158. unset($order_data['status']);
  159. $order_data['amount'] = $freeze?(-$goods_info['price']):$goods_info['price'];
  160. $order_data['img_url']= $goods_info['img_url'];
  161. $this->success('', $order_data);
  162. }
  163. /**
  164. * 提交订单
  165. * @return void
  166. * @throws \think\exception\DbException
  167. */
  168. public function submit()
  169. {
  170. $user = $this->auth->getUser();
  171. $order_no = $this->request->post('order_no');
  172. if(empty($order_no)){
  173. $this->error(__('参数有误'));
  174. }
  175. $order_info = (new OrderModel)
  176. ->where('user_id', $user['id'])
  177. ->where('order_no', $order_no)
  178. ->find();
  179. if(empty($order_info)){
  180. $this->error(__('参数有误'));
  181. }
  182. if($user['balance'] < 0){
  183. $this->error(__('余额不足'));
  184. }
  185. if($user['freeze'] < 0){
  186. $this->error(__('余额不足'));
  187. }
  188. if($order_info['status'] == OrderModel::Success){
  189. $this->success('');
  190. }
  191. // 启动事务
  192. Db::startTrans();
  193. try {
  194. // 更新订单
  195. (new OrderModel)
  196. ->where('id', $order_info['id'])
  197. ->update([
  198. 'status' => OrderModel::Success
  199. ]);
  200. if($order_info['status'] == OrderModel::Default){
  201. //卡单订单不扣款,不返款,直接发佣金
  202. //订单佣金
  203. (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '卡单订单');
  204. }else{
  205. //账变
  206. (new MoneyLog())->change($user['id'], $order_info['amount'], MoneyLog::PayBack, '', '');
  207. //订单佣金
  208. (new MoneyLog())->change($user['id'], $order_info['bonus'], MoneyLog::OrderBonus, '', '');
  209. }
  210. //累积佣金和任务次数
  211. (new Users())
  212. ->where('id', $user['id'])
  213. ->update([
  214. 'task_num' => Db::raw('task_num + 1'),
  215. 'bonus_sum' => Db::raw('bonus_sum + ' .$order_info['bonus']),
  216. 'task_last_time'=> time()
  217. ]);
  218. //向上级发放
  219. // 提交事务
  220. Db::commit();
  221. } catch (Exception $e) {
  222. // 回滚事务
  223. Db::rollback();
  224. $this->error($e->getMessage());
  225. }
  226. $this->success('');
  227. }
  228. }