Coupon.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 Coupon 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. $ganemid = $this->request->get('game_id', '', 'trim');
  21. $name = $this->request->get('name', '', 'trim');
  22. if (isset($name) && !empty($name)) {
  23. $where['name'] = ['like', "%$name%"];
  24. }
  25. $order = 'id desc';
  26. $tmpGameList = model('Common/Game')->getAllByCondition('id,name');
  27. $gameList = array();
  28. foreach ($tmpGameList as $game) {
  29. $gameList[$game['id']] = $game;
  30. }
  31. $couponModel = model('Coupon');
  32. $list = $couponModel
  33. ->where($where)
  34. ->where(function ($query) use ($ganemid) {
  35. if (isset($ganemid) && !empty($ganemid)) {
  36. $query->whereRaw(sprintf('FIND_IN_SET(%s,game_id)', $ganemid));
  37. }
  38. })
  39. ->order($order)
  40. ->paginate(10, false, ['query' => input('get.')])->each(function ($item, $key) use ($gameList) {
  41. if ($item['game_id']) {
  42. $game_ids = explode(',', $item['game_id']);
  43. $tmp = [];
  44. foreach ($game_ids as $k => $v) {
  45. if (isset($gameList[$v])) {
  46. $tmp[] = $gameList[$v]['name'];
  47. }
  48. }
  49. $item['game_name'] = implode(', ', $tmp);
  50. } else {
  51. $item['game_name'] = '全部';
  52. }
  53. return $item;
  54. });
  55. $this->assign('game_list', $tmpGameList);
  56. $this->assign('list', $list);
  57. $this->assign('page', $list->render());
  58. return $this->fetch();
  59. }
  60. /**
  61. *增
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $data = $this->request->post();
  67. $checkResult = $this->validate($data, 'Coupon.add');
  68. if (true !== $checkResult) {
  69. $this->error($checkResult);
  70. }
  71. if ($data['select']) {
  72. $data['game_id'] = $data['select'];
  73. }
  74. unset($data['select']);
  75. $data['update_time'] = $data['create_time'] = time();
  76. if ($data['type_id'] == 1) {
  77. $data['start_time'] = strtotime($data['start_time']);
  78. $data['end_time'] = strtotime($data['end_time']);
  79. $data['day'] = 0;
  80. } else {
  81. $data['start_time'] = 0;
  82. $data['end_time'] = 0;
  83. }
  84. if (model('Coupon')->insertGetId($data)) {
  85. $this->success('添加成功', url('index'));
  86. }
  87. $this->error('添加失败');
  88. }
  89. $games = model('Common/Game')->getAllByCondition('id,name', ['cooperation_status' => ['neq', 3]], 'id desc', 'self');
  90. $this->assign('games', $games);
  91. return $this->fetch();
  92. }
  93. public function edit()
  94. {
  95. $id = $this->request->param('id', '', 'intval');
  96. $appModel = model('Coupon')->where(['id' => $id]);
  97. if (!$data = $appModel->find($id)) {
  98. $this->error('参数错误,不存在该数据');
  99. }
  100. if (empty($id)) {
  101. $this->error('参数错误!');
  102. }
  103. if ($this->request->isPost()) {
  104. $data = $this->request->post();
  105. $checkResult = $this->validate($data, 'Coupon.edit');
  106. if (true !== $checkResult) {
  107. $this->error($checkResult);
  108. }
  109. $data['update_time'] = $data['create_time'] = time();
  110. if ($data['type_id'] == 1) {
  111. $data['start_time'] = strtotime($data['start_time']);
  112. $data['end_time'] = strtotime($data['end_time']);
  113. $data['day'] = 0;
  114. } else {
  115. $data['start_time'] = 0;
  116. $data['end_time'] = 0;
  117. }
  118. $data['update_time'] = time();
  119. if (model('Coupon')->where(['id' => $id])->update($data)) {
  120. $this->success('修改成功', url('index'));
  121. }
  122. $this->error($appModel->getError() ?: '修改失败');
  123. }
  124. if ($data['type_id'] == 1) {
  125. $data['start_time'] = strtotime($data['start_time']);
  126. $data['end_time'] = strtotime($data['end_time']);
  127. $data['day'] = '';
  128. } else {
  129. $data['start_time'] = '';
  130. $data['end_time'] = '';
  131. }
  132. if ($data['game_id']) {
  133. $game = explode(',', $data['game_id']);
  134. $tmpGameList = model('Common/Game')->getAllByCondition('name', ['id' => ['in', $game]]);
  135. $this->assign('game_name', implode(',', array_column($tmpGameList, 'name')));
  136. } else {
  137. $this->assign('game_name', '全部');
  138. }
  139. $this->assign('data', $data);
  140. return $this->fetch();
  141. }
  142. /**
  143. *删
  144. */
  145. public function delete()
  146. {
  147. $id = $this->request->param('id', '', 'intval');
  148. if (empty($id)) {
  149. $this->error('参数错误!');
  150. }
  151. $appModel = model('Common/Coupon');
  152. if (!$info = $appModel->find($id)) {
  153. $this->error('参数错误,不存在该数据');
  154. }
  155. if ($info['grant_num'] > 0) {
  156. $this->error('已存在发放记录 不能删除');
  157. }
  158. if ($appModel->where('id', '=', $id)->delete()) {
  159. $this->success('删除成功', url('index'));
  160. }
  161. $error = $appModel->getError();
  162. $this->error($error ?: '删除失败');
  163. }
  164. public function addCoupon()
  165. {
  166. if (!$id = input('id')) {
  167. $this->error('记录不存在');
  168. }
  169. $coupon = model('Common/Coupon')->where('id', $id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)',Db::quote(date('Y-m-d'))))->find();
  170. if (!$coupon) {
  171. $this->error('记录不存在');
  172. }
  173. $this->assign('id', $id);
  174. return $this->fetch();
  175. }
  176. public function addCouponSave()
  177. {
  178. if (!$id = input('id')) {
  179. $this->error('记录不存在');
  180. }
  181. $coupon = model('Common/Coupon')->where('id', $id)->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2)',Db::quote(date('Y-m-d'))))->find();
  182. if (!$coupon) {
  183. $this->error('记录不存在');
  184. }
  185. if (!$number = (int)input('number')) {
  186. $this->error('数量不存在');
  187. }
  188. if ($number <= 0) {
  189. $this->error('数量错误');
  190. }
  191. for ($i = 0; $i < $number; $i++) {
  192. $data[] = [
  193. 'coupon_id' => $coupon['id'],
  194. 'examine' => 2,
  195. 'code' => makeUniqueid('Q'),
  196. ];
  197. }
  198. model('Common/Coupon')->startTrans();
  199. if(!model('Common/Coupon')->where('id', $id)->update(['grant_num' => $coupon['grant_num']+$number])){
  200. model('Common/Coupon')->rollback();
  201. $this->jsonResult('', 0, '生成失败');
  202. }
  203. if (model('couponMember')->insertAll($data)) {
  204. model('Common/Coupon')->commit();
  205. $this->jsonResult('', 1, '生成成功');
  206. } else {
  207. model('Common/Coupon')->rollback();
  208. $this->jsonResult('', 0, '生成失败');
  209. }
  210. }
  211. public function coupon(){
  212. $where = [];
  213. //查询参数
  214. $username = input('username');
  215. $name = input('name');
  216. $id = input('id');
  217. $is_use = input('is_use');
  218. $state = input('state');
  219. if (isset($id) && !empty($id)) {
  220. $where['a.id'] = $id;
  221. }
  222. if (isset($name) && !empty($name)) {
  223. $where['a.name'] = ['like', "$name%"];
  224. }
  225. if (isset($username) && !empty($username)) {
  226. $where['b.username'] = $username;
  227. }
  228. if (isset($is_use) && !empty($is_use)) {
  229. $where['c.is_use'] = $is_use;
  230. }
  231. if (isset($state) && !empty($state)) {
  232. $where['c.state'] = $state;
  233. }
  234. $couponModel = model('Coupon');
  235. $list = $couponModel
  236. ->alias('a')
  237. ->join('cy_coupon_member c', 'a.id=c.coupon_id','left')
  238. ->join('cy_members b', 'c.member_id=b.id','left')
  239. ->where($where)
  240. ->field('c.*,b.username,a.name,a.money,a.min_money,a.start_time as coupon_start_time,a.end_time as coupon_end_time,a.type_id')
  241. ->order('id desc')
  242. ->paginate(10, false, ['query' => input()])->each(function ($item, $key) {
  243. if($item['use_time']) {
  244. $item['use_time'] = date('Y-m-d H:i:s',$item['use_time']);
  245. }
  246. if($item['use_time'] == 0) {
  247. $item['use_time'] = "";
  248. }
  249. $dayTime = strtotime(date('Y-m-d'));
  250. // 类型:1有效时间, 2领取后多少天内有效
  251. if ($item['type_id'] == 2) {
  252. if ($item['member_id'] > 0 && $item['end_time'] < $dayTime) {
  253. $item['state_str'] = '<span style="color: red">(已过期)</span>';
  254. } else {
  255. $item['state_str'] = '';
  256. }
  257. } else {
  258. if ($item['coupon_end_time'] < $dayTime) {
  259. $item['state_str'] = '<span style="color: red">(已过期)</span>';
  260. } else {
  261. $item['state_str'] = '';
  262. }
  263. }
  264. return $item;
  265. });
  266. $this->assign('list', $list);
  267. $this->assign('page', $list->render());
  268. return $this->fetch();
  269. }
  270. public function frozen()
  271. {
  272. $id = input('id');
  273. $coupon_id = input('coupon_id');
  274. $coupon = model('Common/CouponMember')->where('id', $id)->find();
  275. if (!$coupon) {
  276. $this->error('记录不存在');
  277. }
  278. if ($coupon['state'] == 2) {
  279. $this->success('冻结成功', url('index'));
  280. }
  281. if (model('common/CouponMember')->where('id', $id)->update(['state' => 2])) {
  282. if($coupon_id){
  283. $this->success('冻结成功', url('Coupon/coupon', ['id'=> $coupon_id]));
  284. }else{
  285. $this->success('冻结成功', url('Coupon/index'));
  286. }
  287. } else {
  288. $this->error('冻结失败');
  289. }
  290. }
  291. public function thaw()
  292. {
  293. $id = input('id');
  294. $coupon_id = input('coupon_id');
  295. $coupon = model('common/CouponMember')->where('id', $id)->find();
  296. if (!$coupon) {
  297. $this->error('记录不存在');
  298. }
  299. if ($coupon['state'] == 1) {
  300. $this->success('解冻成功', url('index'));
  301. }
  302. if (model('Common/CouponMember')->where('id', $id)->update(['state' => 1])) {
  303. if($coupon_id){
  304. $this->success('解冻成功', url('Coupon/coupon', ['id'=> $coupon_id]));
  305. }else{
  306. $this->success('解冻成功', url('Coupon/index'));
  307. }
  308. } else {
  309. $this->error('解冻失败');
  310. }
  311. }
  312. public function approved()
  313. {
  314. $data = input();
  315. $state = $data['state'];
  316. $ids = explode(',', $data['ids']);
  317. unset($data['ids']);
  318. if (empty($state)) {
  319. $this->error("系统错误");
  320. }
  321. if (!in_array($state, [1, 2])) {
  322. $this->error('状态错误');
  323. }
  324. if (empty($ids)) {
  325. $this->error("请选择记录");
  326. }
  327. $data = model('common/CouponMember')->whereIn('id', $ids)->column('id');
  328. if ($data) {
  329. if (model('common/CouponMember')->whereIn('id', $data)->update(['state' => $state])) {
  330. $this->success("审核成功");
  331. } else {
  332. $this->error('审核失败');
  333. }
  334. }
  335. }
  336. }