usership.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace WY\app\controller\derpay;
  3. use WY\app\libs\Controller;
  4. use WY\app\model\Bill;
  5. if (!defined('WY_ROOT')) {
  6. exit;
  7. }
  8. class usership extends CheckAdmin
  9. {
  10. public function index()
  11. {
  12. $kw = $this->req->get('kw');
  13. $ship_type = $this->req->get('ship_type');
  14. $ship_cycle = $this->req->get('ship_cycle');
  15. $ship_type = isset($_GET['ship_type']) ? $ship_type : -1;
  16. $ship_cycle = isset($_GET['ship_cycle']) ? $ship_cycle : -1;
  17. $cons = 'a.is_state=? and a.is_agent=?';
  18. $consOR = '';
  19. $consArr = array('1', 0);
  20. if ($kw) {
  21. $consOR .= $consOR ? ' or ' : '';
  22. $consOR .= 'a.username like ?';
  23. $consArr[] = '%' . $kw . '%';
  24. }
  25. if ($kw) {
  26. $consOR .= $consOR ? ' or ' : '';
  27. $consOR .= 'a.id = ?';
  28. $consArr[] = $kw;
  29. }
  30. if ($consOR) {
  31. $cons .= $cons ? ' and ' : '';
  32. $cons .= '(' . $consOR . ')';
  33. }
  34. if ($ship_type >= 0) {
  35. $cons .= $cons ? ' and ' : '';
  36. $cons .= 'a.ship_type=?';
  37. $consArr[] = $ship_type;
  38. }
  39. if ($ship_cycle >= 0) {
  40. $cons .= $cons ? ' and ' : '';
  41. $cons .= 'a.ship_cycle=?';
  42. $consArr[] = $ship_cycle;
  43. }
  44. $orderby = 'id desc';
  45. $sort = $this->req->get('sort');
  46. $sort = isset($_GET['sort']) ? $sort : 0;
  47. $by = $this->req->get('by');
  48. if ($by) {
  49. $sort2 = $sort ? ' desc' : ' asc';
  50. $orderby = $by . $sort2;
  51. }
  52. $page = $this->req->get('p');
  53. $page = $page ? $page : 1;
  54. $pagesize = 15;
  55. $totalsize = $this->model()->select()->from('users a')->where(array('fields' => $cons, 'values' => $consArr))->count();
  56. $lists = array();
  57. if ($totalsize) {
  58. $totalpage = ceil($totalsize / $pagesize);
  59. $page = $page > $totalpage ? $totalpage : $page;
  60. $offset = ($page - 1) * $pagesize;
  61. $lists = $this->model()->select()->from('users a')->offset($offset)->limit($pagesize)->orderby($orderby)->where(array('fields' => $cons, 'values' => $consArr))->fetchAll();
  62. /*$cons.=' and b.is_state=? and b.is_ship=? and b.addtime<?';$consArr=array_merge($consArr,array(1,0,strtotime(date('Y-m-d'))));$lists=$this->model()->select('a.*,count(b.id) as total_unship_count,sum(b.realmoney*b.uprice) as income')->from('users a')->left('orders b')->on('a.id=b.userid')->join()->offset($offset)->limit($pagesize)->where(array('fields'=>$cons,'values'=>$consArr))->groupby('a.id')->orderby($orderby)->fetchAll();*/
  63. }
  64. $pagelist = $this->page->put(array('page' => $page, 'pagesize' => $pagesize, 'totalsize' => $totalsize, 'url' => '?ship_type=' . $ship_type . '&ship_cycle=' . $ship_cycle . '&kw=' . $kw . '&sort=' . $sort . '&by=' . $by . '&p='));
  65. $data = array('title' => '用户列表', 'lists' => $lists, 'pagelist' => $pagelist, 'search' => array('kw' => $kw, 'ship_type' => $ship_type, 'ship_cycle' => $ship_cycle), 'sort' => $sort, 'by' => $by);
  66. $this->put('usership.php', $data);
  67. }
  68. public function ship()
  69. {
  70. $bill = new Bill();
  71. $userid = isset($this->action[3]) ? $this->action[3] : 0;
  72. $users = $this->model()->select('unpaid')->from('users')->where(array('fields' => 'id=?', 'values' => array($userid)))->fetchRow();
  73. $newMoney = $users['unpaid'];
  74. $income = $bill->beforeUserIncome($userid, 0);
  75. if ($income > 0) {
  76. $newMoney += $income;
  77. $data = array('unpaid' => $newMoney);
  78. if ($this->model()->from('users')->updateSet($data)->where(array('fields' => 'id=?', 'values' => array($userid)))->update()) {
  79. $where = array('fields' => 'is_state=? and is_ship=? and userid=? and addtime<?', 'values' => array(1, 0, $userid, strtotime(date('Y-m-d'))));
  80. $this->model()->from('orders')->updateSet(array('is_ship' => 1))->where($where)->update();
  81. $data = array('userid' => $userid, 'is_agent' => 0, 'before_money' => $users['unpaid'], 'money' => $income, 'after_money' => $newMoney, 'ctype' => '结算', 'addtime' => time());
  82. $this->model()->from('paylogs')->insertData($data)->insert();
  83. }
  84. }
  85. $userinfo = $this->model()->select()->from('userinfo')->where(array('fields' => 'userid=?', 'values' => array($userid)))->fetchRow();
  86. $userinfo += array('money' => $newMoney, 'fee' => $bill->fee($newMoney));
  87. $this->put('usershippay.php', $userinfo);
  88. }
  89. public function savepay()
  90. {
  91. $userid = isset($this->action[3]) ? $this->action[3] : 0;
  92. $data = isset($_POST) ? $_POST : false;
  93. if (!$data || !$userid) {
  94. $this->put('woodyapp.php', array('msg' => '数据出现错误'));
  95. exit;
  96. }
  97. $users = $this->model()->select('paid,unpaid')->from('users')->where(array('fields' => 'id=?', 'values' => array($userid)))->fetchRow();
  98. if (!$users) {
  99. $this->put('woodyapp.php', array('msg' => '用户不存在'));
  100. exit;
  101. }
  102. if ($users['unpaid'] < $data['money'] || $data['money'] <= 0) {
  103. $this->put('woodyapp.php', array('msg' => '账户余额不足'));
  104. exit;
  105. }
  106. $data += array('addtime' => time(), 'lastime' => time(), 'userid' => $userid);
  107. if ($this->model()->from('payments')->insertData($data)->insert()) {
  108. $newMoney = $users['unpaid'] - $data['money'];
  109. $data = array('userid' => $userid, 'is_agent' => 0, 'before_money' => $users['unpaid'], 'money' => $data['money'], 'after_money' => $newMoney, 'ctype' => '付款', 'addtime' => time());
  110. $this->model()->from('paylogs')->insertData($data)->insert();
  111. $newData = array('paid' => $users['paid'] + $data['money'], 'unpaid' => $newMoney);
  112. $this->model()->from('users')->updateSet($newData)->where(array('fields' => 'id=?', 'values' => array($userid)))->update();
  113. $this->put('woodyapp.php', array('msg' => '付款记录已成功创建'));
  114. exit;
  115. }
  116. $this->put('woodyapp.php', array('msg' => '付款失败'));
  117. exit;
  118. }
  119. public function batch()
  120. {
  121. $this->put('usershipbatch.php');
  122. }
  123. public function batchsave()
  124. {
  125. $sim = $this->req->post('sim');
  126. $money = $this->req->post('money');
  127. if ($ship_cycle >= 0) {
  128. $users = $this->model()->select('id,paid,unpaid')->from('users')->where(array('fields' => 'is_agent=? and is_state=? and ship_type=? and ship_cycle=?', 'values' => array(0, 1, 0)))->fetchAll();
  129. } else {
  130. $users = $this->model()->select('id,paid,unpaid')->from('users')->where(array('fields' => 'is_agent=? and is_state=? and ship_type=?', 'values' => array(0, 1)))->fetchAll();
  131. }
  132. $i = 0;
  133. if ($users) {
  134. $bill = new Bill();
  135. foreach ($users as $key => $user) {
  136. $userid = $user['id'];
  137. $fromTime = strtotime(date('Y-m-d') . ' 23:59:59') - 60 * 60 * 24 * 1;
  138. $unpaid = $user['unpaid'];
  139. $income = $bill->getUserIncome($userid, $fromTime);
  140. $total_income = $unpaid + $income;
  141. if ($total_income > 0 && $sim == '1' ? $total_income > $money : $total_income < $money) {
  142. $i++;
  143. if ($this->model()->from('users')->updateSet(array('unpaid' => $total_income))->where(array('fields' => 'id=?', 'values' => array($userid)))->update()) {
  144. $where = array('fields' => 'is_state=? and is_ship=? and userid=? and addtime<=?', 'values' => array(1, 0, $userid, $fromTime));
  145. $this->model()->from('orders')->updateSet(array('is_ship' => 1))->where($where)->update();
  146. $data = array('userid' => $userid, 'is_agent' => 0, 'before_money' => $unpaid, 'money' => $income, 'after_money' => $total_income, 'ctype' => '结算', 'addtime' => time());
  147. $this->model()->from('paylogs')->insertData($data)->insert();
  148. $userinfo = $this->model()->select()->from('userinfo')->where(array('fields' => 'userid=?', 'values' => array($userid)))->fetchRow();
  149. $data = array('sn' => 'b' . time(), 'money' => $total_income, 'fee' => $bill->fee($total_income), 'realname' => $userinfo['realname'], 'batype' => $userinfo['batype'], 'baname' => $userinfo['baname'], 'addtime' => time(), 'lastime' => time(), 'userid' => $userid, 'remark' => '批量结算');
  150. if ($this->model()->from('payments')->insertData($data)->insert()) {
  151. $newMoney = 0;
  152. $data = array('userid' => $userid, 'is_agent' => 0, 'before_money' => $total_income, 'money' => $total_income, 'after_money' => $newMoney, 'ctype' => '付款', 'addtime' => time());
  153. $this->model()->from('paylogs')->insertData($data)->insert();
  154. $newData = array('paid' => $user['paid'] + $total_income, 'unpaid' => $newMoney);
  155. $this->model()->from('users')->updateSet($newData)->where(array('fields' => 'id=?', 'values' => array($userid)))->update();
  156. }
  157. }
  158. }
  159. }
  160. }
  161. $this->put('woodyapp.php', array('msg' => '批量结算完成(共结算' . $i . '个用户)', 'url' => $this->dir . 'userpay'));
  162. }
  163. }