CouponMember.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\ComplexMembers as MembersModel;
  4. use app\common\model\ComplexChannelModel;
  5. use app\common\model\ComplexPay;
  6. use GuzzleHttp\Exception\RequestException;
  7. use think\Db;
  8. use app\common\library\MakeReport;
  9. use GuzzleHttp\Client;
  10. class CouponMember extends Admin
  11. {
  12. public function _initialize()
  13. {
  14. parent::_initialize(); // TODO: Change the autogenerated stub
  15. }
  16. public function index()
  17. {
  18. $where = [];
  19. //查询参数
  20. $username = input('username');
  21. $name = input('name');
  22. $is_use = input('is_use');
  23. $state = input('state');
  24. if (isset($name) && !empty($name)) {
  25. $where['c.name'] = ['like', "$name%"];
  26. }
  27. if (isset($username) && !empty($username)) {
  28. $where['b.username'] = $username;
  29. }
  30. if (isset($is_use) && !empty($is_use)) {
  31. $where['a.is_use'] = $is_use;
  32. }
  33. if (isset($state) && !empty($state)) {
  34. $where['a.state'] = $state;
  35. }
  36. $couponMemberModel = model('CouponMember');
  37. $list = $couponMemberModel
  38. ->alias('a')
  39. ->join('cy_members b', 'a.member_id=b.id')
  40. ->join('cy_coupon c', 'a.coupon_id=c.id')
  41. ->where($where)
  42. ->field('a.*,b.username,c.name,c.money,c.min_money,c.start_time as coupon_start_time,c.end_time as coupon_end_time,c.type_id,a.yql_id')
  43. ->order('id desc')
  44. ->paginate(10, false, ['query' => input('get.')])->each(function ($item, $key) {
  45. $dayTime = strtotime(date('Y-m-d'));
  46. if ($item['type_id'] == 1) {
  47. if ($item['coupon_end_time'] < $dayTime) {
  48. $item['state_str'] = '<span style="color: red">(已过期.)</span>';
  49. } else {
  50. $item['state_str'] = '';
  51. }
  52. } else {
  53. if (strtotime($item['end_time']) < $dayTime) {
  54. $item['state_str'] = '<span style="color: red">(已过期)</span>';
  55. } else {
  56. $item['state_str'] = '';
  57. }
  58. }
  59. return $item;
  60. });
  61. $this->assign('list', $list);
  62. $this->assign('page', $list->render());
  63. return $this->fetch();
  64. }
  65. /**
  66. *增
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $data = $this->request->post();
  72. $coupon = model('Common/Coupon')->where('id', $data['coupon_id'])
  73. ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)', Db::quote(date('Y-m-d'))))->find();
  74. if (!$coupon) {
  75. $this->error('记录不存在');
  76. }
  77. $member_ids = explode("\r\n", $data['member_ids']);
  78. $member_ids = array_map('filterAndTrimInput', $member_ids);
  79. $member_ids = array_unique(array_filter($member_ids));
  80. $member = model('Members')->whereIn('username', $member_ids)->column('id', 'username');
  81. $arr = [];
  82. foreach ($member_ids as $k => $v) {
  83. if (isset($member[$v])) {
  84. $arr[] = [
  85. 'username' => $v,
  86. 'state' => '<span style="color: #0bb20c">正常</span>'
  87. ];
  88. } else {
  89. $arr[] = [
  90. 'username' => $v,
  91. 'state' => '<span style="color: red">账号不存在</span>'
  92. ];
  93. }
  94. }
  95. $this->assign('ids', implode(',', $member));
  96. $this->assign('members', $arr);
  97. $this->assign('coupon', $coupon);
  98. return $this->fetch('confirm');
  99. }
  100. $coupon = model('Coupon')->where('state', 1)
  101. ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)', Db::quote(date('Y-m-d'))))
  102. ->order('id desc')->select();
  103. $this->assign('coupon', $coupon);
  104. return $this->fetch();
  105. }
  106. public function confirm()
  107. {
  108. $data = $this->request->post();
  109. if (!$data['ids']) {
  110. $this->error('账号不存在');
  111. }
  112. if (!$data['coupon_id']) {
  113. $this->error('代金券不存在');
  114. }
  115. $coupon = model('Common/Coupon')->where('id', $data['coupon_id'])
  116. ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)', Db::quote(date('Y-m-d'))))->find();
  117. if (!$coupon) {
  118. $this->error('记录不存在');
  119. }
  120. $member_ids = array_map('filterAndTrimInput', explode(',', $data['ids']));
  121. $member_ids = array_unique(array_filter($member_ids));
  122. $member = model('Members')->whereIn('id', $member_ids)->column('id');
  123. if (!$member) {
  124. $this->error('账号不存在');
  125. }
  126. $members = [];
  127. if ($coupon['type_id'] == 1) {
  128. $start_time = 0;
  129. $end_time = 0;
  130. } else {
  131. $start_time = strtotime(date('Y-m-d'));
  132. $end_time = strtotime(date('Y-m-d')) + 86400 * $coupon['day'] - 1;
  133. }
  134. foreach ($member as $k => $v) {
  135. $members[] = [
  136. 'coupon_id' => $coupon['id'],
  137. 'member_id' => $v,
  138. 'start_time' => $start_time,
  139. 'end_time' => $end_time,
  140. 'examine' => 2,
  141. 'code' => makeUniqueid('Q') . 'A',
  142. ];
  143. }
  144. model('Common/Coupon')->startTrans();
  145. if (!model('Common/Coupon')->where('id', $coupon['id'])->update(['grant_num' => $coupon['grant_num'] + count($member),'receive_num' => $coupon['receive_num'] + count($member)])) {
  146. model('Common/Coupon')->rollback();
  147. $this->error('生成失败');
  148. }
  149. if (model('couponMember')->insertAll($members)) {
  150. model('Common/Coupon')->commit();
  151. $this->success('生成成功', url('index'));
  152. } else {
  153. model('Common/Coupon')->rollback();
  154. $this->error('生成失败');
  155. }
  156. }
  157. public function frozen()
  158. {
  159. $id = input('id');
  160. $coupon = model('Common/CouponMember')->where('id', $id)->find();
  161. if (!$coupon) {
  162. $this->error('记录不存在');
  163. }
  164. if ($coupon['state'] == 2) {
  165. $this->success('冻结成功', url('index'));
  166. }
  167. if (model('common/CouponMember')->where('id', $id)->update(['state' => 2])) {
  168. $this->success('冻结成功', url('index'));
  169. } else {
  170. $this->error('冻结失败');
  171. }
  172. }
  173. public function thaw()
  174. {
  175. $id = input('id');
  176. $coupon = model('common/CouponMember')->where('id', $id)->find();
  177. if (!$coupon) {
  178. $this->error('记录不存在');
  179. }
  180. if ($coupon['state'] == 1) {
  181. $this->success('解冻成功', url('index'));
  182. }
  183. if (model('Common/CouponMember')->where('id', $id)->update(['state' => 1])) {
  184. $this->success('解冻成功', url('index'));
  185. } else {
  186. $this->error('解冻失败');
  187. }
  188. }
  189. public function approved()
  190. {
  191. $data = input();
  192. $state = $data['state'];
  193. $ids = explode(',', $data['ids']);
  194. unset($data['ids']);
  195. if (empty($state)) {
  196. $this->error("系统错误");
  197. }
  198. if (!in_array($state, [1, 2])) {
  199. $this->error('状态错误');
  200. }
  201. if (empty($ids)) {
  202. $this->error("请选择记录");
  203. }
  204. $data = model('common/CouponMember')->whereIn('id', $ids)->column('id');
  205. if ($data) {
  206. if (model('common/CouponMember')->whereIn('id', $data)->update(['state' => $state])) {
  207. $this->success("审核成功");
  208. } else {
  209. $this->error('审核失败');
  210. }
  211. }
  212. }
  213. }