ActivityApplyItem.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <?php
  2. namespace app\operate\controller;
  3. use app\common\model\BillApply;
  4. use app\common\model\StoreOrderRefund;
  5. use app\common\model\UserLearn;
  6. use app\common\model\UserMessage;
  7. use app\common\service\OrderCallback;
  8. use app\common\service\UserSynth;
  9. use library\Controller;
  10. use think\Db;
  11. /**
  12. * 活动
  13. * Class ActivityApplyItem
  14. * @package app\operate\controller
  15. */
  16. class ActivityApplyItem extends Controller
  17. {
  18. protected $table = 'ActivityApplyItem';
  19. /**
  20. * 列表
  21. * @auth true
  22. * @menu true
  23. * @throws \think\Exception
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. * @throws \think\exception\PDOException
  28. */
  29. public function index()
  30. {
  31. $this->status_list = [0=>'待审核',1=>'审核通过',2=>'审核拒绝'];
  32. $this->sh_status = $this->request->get('sh_status',-1);
  33. $id = $this->request->get('act_id');
  34. $name = $this->request->get('name');
  35. $phone = $this->request->get('phone');
  36. $qr_content = $this->request->get('qr_content');
  37. $sh_status = $this->request->get('sh_status',-1);
  38. $this->title = '报名记录';
  39. $where = [];
  40. $where[]= ['a.act_id','=' ,$id];
  41. $where[]= ['a.is_deleted','=' ,0];
  42. $where[]= ['o.pay_state','=' ,1];
  43. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  44. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  45. if($qr_content) $where[]= ['a.qr_content','like' ,'%'.$phone.'%'];
  46. if(in_array($sh_status,[0,1,2])) $where[]= ['a.sh_status','=' ,$sh_status];
  47. $query = $this->_query('activity_apply_item')
  48. ->alias('a')
  49. ->field('a.*,u.headimg,o.order_no')
  50. ->where($where)
  51. ->leftJoin('store_member u','u.id = a.user_id')
  52. ->leftJoin('activity_apply o','o.id = a.apply_id')
  53. ->order('a.id desc')->page();
  54. }
  55. protected function _index_page_filter(array &$data)
  56. {
  57. foreach ($data as &$vo) {
  58. $vo['refund_status'] = StoreOrderRefund::where('order_type',1)->where('order_id',$vo['apply_id'])->where('item_id',$vo['id'])->value('status');
  59. }
  60. }
  61. /**
  62. * 编辑
  63. * @auth true
  64. * @menu true
  65. * @throws \think\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. * @throws \think\exception\PDOException
  70. */
  71. public function edit()
  72. {
  73. $this->title = '编辑';
  74. $this->_form($this->table, 'form');
  75. }
  76. /**
  77. * 删除
  78. * @auth true
  79. * @throws \think\Exception
  80. * @throws \think\exception\PDOException
  81. */
  82. public function del()
  83. {
  84. $this->_save($this->table, ['is_deleted' => '1']);
  85. }
  86. /**
  87. * 批量删除
  88. * @auth true
  89. * @throws \think\Exception
  90. * @throws \think\exception\PDOException
  91. */
  92. public function remove()
  93. {
  94. $this->_save($this->table, ['is_deleted' => '1']);
  95. }
  96. /**
  97. * 表单数据处理
  98. * @auth true
  99. * @menu true
  100. * @param array $data
  101. */
  102. protected function _form_filter(&$data)
  103. {
  104. if($this->request->isGet() && $this->request->action() == 'edit')
  105. {
  106. if(isset($data['extend'])) $data['extend'] = json_decode($data['extend'],true);
  107. }
  108. if($this->request->isPost() && $this->request->action() == 'edit')
  109. {
  110. $ticket_info = \app\common\model\ActivityApplyItem::where('id',$data['id'])->find()->toArray();
  111. // 审核成功
  112. if($data['sh_status'] == 1)
  113. {
  114. if($data['sh_status'] == 1 && $ticket_info['sh_status'] !=1)
  115. {
  116. if(!$ticket_info['qr_code']) {
  117. $qrCode = new \Endroid\QrCode\QrCode();
  118. // 生成核销码
  119. $qr_content = $ticket_info['act_id'].'-'.$ticket_info['apply_id'].'-'.$ticket_info['id'];
  120. $qrCode->setText($qr_content);
  121. $qrCode->setSize(200);
  122. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/code';
  123. $filename = $dir.'/'.$ticket_info['id'].'.png';
  124. $qrCode->writeFile($filename);
  125. $url='http://'.$_SERVER['SERVER_NAME']."/code/".$ticket_info['id'].'.png';
  126. \app\common\model\ActivityApplyItem::where('id',$ticket_info['id'])->update(['qr_code'=>$url,'qr_content'=>$qr_content]);
  127. }
  128. $act_title = \app\common\model\Activity::where('id',$ticket_info['act_id'])->value('title');
  129. $message = '您已成功报名"'.$act_title.'"';//点击<a href=\"$info_url\">查看详情及点子门票</a>";
  130. UserMessage::sendUserMessage($ticket_info['user_id'],'activity',1,1,0,$ticket_info['act_id'],$message,$ticket_info['id']);
  131. UserSynth::ticketSend($ticket_info['id']);
  132. //以下为新增内容
  133. $act = \app\common\model\Activity::where('id',$ticket_info['act_id'])->find();
  134. $nextYear = date('Y-m-d H:i:s', strtotime('+1 year'));
  135. //视频
  136. if($act['video_id'] && $act['video_id'] != 0){
  137. $arr = [
  138. 'user_id' => $ticket_info['user_id'],
  139. 'type' => 1,
  140. 'first_id' => $act['video_id'],
  141. 'second_id' => $act['video_item'],
  142. 'end_time' => $nextYear,
  143. 'end_int' => strtotime($nextYear)
  144. ];
  145. if(!UserLearn::where('type',1)->where('first_id',$act['datum_id'])->where('user_id',$ticket_info['user_id'])->find()) {
  146. UserLearn::create($arr);
  147. }
  148. }
  149. //资料
  150. if($act['datum_id'] && $act['datum_id'] != 0){
  151. $arr = [
  152. 'user_id' => $ticket_info['user_id'],
  153. 'type' => 2,
  154. 'first_id' => $act['datum_id'],
  155. 'second_id' => 0,
  156. 'end_time' => $nextYear,
  157. 'end_int' => strtotime($nextYear)
  158. ];
  159. if(!UserLearn::where('type',2)->where('first_id',$act['datum_id'])->where('user_id',$ticket_info['user_id'])->find()){
  160. UserLearn::create($arr);
  161. }
  162. }
  163. //图文
  164. if($act['article_id'] && $act['article_id'] != 0){
  165. $arr = [
  166. 'user_id' => $ticket_info['user_id'],
  167. 'type' => 3,
  168. 'first_id' => $act['article_id'],
  169. 'second_id' => $act['article_item'],
  170. 'end_time' => $nextYear,
  171. 'end_int' => strtotime($nextYear)
  172. ];
  173. if(!UserLearn::where('type',3)->where('first_id',$act['datum_id'])->where('user_id',$ticket_info['user_id'])->find()) {
  174. UserLearn::create($arr);
  175. }
  176. }
  177. //新增内容end
  178. }
  179. if($ticket_info['sh_status'] != 1 && $ticket_info['phone'] && $ticket_info['phone_pre'] == '86') {
  180. $act_info = \app\common\model\Activity::where('id',$ticket_info['act_id'])->find()->toArray();
  181. UserSynth::phoneMessageSend($ticket_info['phone'],1, [
  182. 'ActivityName'=>$act_info['title'],
  183. 'url'=>'dist/#/activity_ticket_info?id='.$ticket_info['id'],
  184. ]);
  185. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity_ticket_info?id='.$ticket_info['id'];
  186. }
  187. }
  188. // 审核拒绝
  189. if($data['sh_status'] == 2) {
  190. $order_id = \app\common\model\ActivityApplyItem::where('id',$data['id'])->value('apply_id');
  191. $res = \app\common\service\Activity:: orderApplyRefund($order_id,$data['id'],2,1);
  192. $info_url = 'https://'.$_SERVER['HTTP_HOST'].'/dist/#/activity-detail?id='.$ticket_info['act_id'];
  193. $act_title = \app\common\model\Activity::where('id',$ticket_info['act_id'])->value('title');
  194. $message = '很抱歉,您报名的"'.$act_title.'"审核未通过';//,点击<a href=\"$info_url\">重新报名</a>";
  195. UserMessage::sendUserMessage($ticket_info['user_id'],'activity',1,1,0,$ticket_info['act_id'],$message,$ticket_info['id']);
  196. }
  197. }
  198. }
  199. /**
  200. * 确认收款
  201. * @auth true
  202. * @throws \think\Exception
  203. * @throws \think\exception\PDOException
  204. */
  205. public function confirm()
  206. {
  207. $order_info = \app\common\model\ActivityApply::where('id',input('id'))->find()->toArray();
  208. $back_res = OrderCallback::activityOrderCallBack($order_info);// 支付完成后回调
  209. if(!$back_res['ret_val']) $this->error($back_res['msg']);
  210. $this->success('操作成功');
  211. }
  212. /**
  213. * 退款
  214. * @auth true
  215. * @menu true
  216. * @throws \think\Exception
  217. * @throws \think\db\exception\DataNotFoundException
  218. * @throws \think\db\exception\ModelNotFoundException
  219. * @throws \think\exception\DbException
  220. * @throws \think\exception\PDOException
  221. */
  222. public function refund()
  223. {
  224. $ticket_info = \app\common\model\ActivityApplyItem::where('id',input('id'))->find()->toArray();
  225. $refund_id = StoreOrderRefund::where('order_type',1)->where('order_id',$ticket_info['apply_id'])->where('item_id',$ticket_info['id'])->value('id');
  226. $back_res = \app\common\service\Activity:: orderRefund($refund_id);// 支付完成后回调
  227. if($back_res['code'] != 200) $this->error($back_res['msg']);
  228. $this->success('操作成功');
  229. }
  230. /**
  231. * 核销
  232. * @auth true
  233. * @menu true
  234. * @throws \think\Exception
  235. * @throws \think\db\exception\DataNotFoundException
  236. * @throws \think\db\exception\ModelNotFoundException
  237. * @throws \think\exception\DbException
  238. * @throws \think\exception\PDOException
  239. */
  240. public function hx()
  241. {
  242. if($this->request->isPost()) {
  243. $qr_content = input('post.qr_content');
  244. if(!$qr_content) $this->error('没有核销内容');
  245. $item_info = \app\common\model\ActivityApplyItem::where(['i.qr_content'=>$qr_content,'is_hx'=>0])
  246. ->alias('i')->field('i.is_hx,i.status,i.apply_id,i.is_deleted,a.status,a.cancel_state,a.pay_state,a.refund_state')
  247. ->leftJoin("ActivityApply a",'a.id = i.apply_id')->find();
  248. if(!$item_info) $this->error('没有找到核销记录');
  249. if($item_info->pay_state)$this->error('订单已取消');
  250. $refund_info = StoreOrderRefund::getRefundInfo($item_info['apply_id'],$item_info['id'],1);
  251. if(!empty($refund_info && in_array($refund_info['status'],[0,1,3,5])))$this->error('已申请退款');
  252. //if(!in_array($item_info->refund_state,[0,3]))$this->error('订单已申请退款');
  253. if(!in_array($item_info->status,[1]))$this->error('票状态异常');
  254. \app\common\model\ActivityApplyItem::where('id',$item_info['id'])->update(['is_hx'=>1,'ticket_status'=>1]);
  255. $this->success('核销成功');
  256. }
  257. $this->_form($this->table, 'hx');
  258. }
  259. public function export(){
  260. $this->order_status = $this->request->get('order_status',-1);
  261. $id = $this->request->get('act_id');
  262. $name = $this->request->get('name');
  263. $phone = $this->request->get('phone');
  264. $source_type = $this->request->get('source_type');
  265. $order_status = $this->request->get('order_status',-1);
  266. $tg_id = $this->request->get('tg_id',0);
  267. $where[]= ['a.act_id','=' ,$id];
  268. $where[]= ['a.is_deleted','=' ,0];
  269. if($name) $where[]= ['a.name','like' ,'%'.$name.'%'];
  270. if($phone) $where[]= ['a.phone','like' ,'%'.$phone.'%'];
  271. if($tg_id) $where[]= ['a.tg_id','=' ,$tg_id];
  272. if(in_array($order_status,[0,1,2])) $where[]= ['a.status','=' ,$order_status];
  273. if($source_type == 1) $where[]= ['a.pay_type','<>' ,9];
  274. if($source_type == 2) $where[]= ['a.pay_type','=' ,9];
  275. $data =Db::name('activity_apply')
  276. ->alias('a')
  277. ->field('a.*,u.name user_name,u.headimg,u.phone,t.phone tg_phone,t.qrcode,y.title act_title')
  278. ->where($where)
  279. ->leftJoin('store_member u','u.id = a.user_id')
  280. ->leftJoin('activity_qrcode t','t.id = a.tg_id')
  281. ->leftJoin('activity y','y.id = a.act_id')
  282. ->order('a.id desc')->select();
  283. if(empty($data)) $this->error('暂无可以导出的数据');
  284. $all = [];
  285. foreach ($data as $k=>&$v)
  286. {
  287. $items = Db::name('activity_apply_item')->where('apply_id',$v['id'])->select();
  288. foreach ($items as $k => $item){
  289. $v['order_source'] = $v['pay_type'] == 9 ? '转账订单':'线上订单';
  290. $v['pay_desc'] = $v['pay_state'] == 1 ? '已支付':'未支付';
  291. $v['i_name'] = $item['name'];
  292. $v['i_phone'] = $item['phone'];
  293. $v['i_email'] = $item['email'];
  294. $v['i_extend'] = '';
  295. $extend = json_decode($item['extend'],true);
  296. if($extend){
  297. foreach ($extend as $key => $val){
  298. $v['i_extend'] .= '问题:'.$val['title'].'
  299. ';
  300. if(isset($val['extend'])){
  301. $v['i_extend'] .= '选项:';
  302. foreach ($val['extend'] as $val1){
  303. $v['i_extend'] .= ''.$val1['item_title'].', ';
  304. }
  305. }else{
  306. $v['i_extend'] .= '回答:'.$val['value'].'
  307. ';
  308. }
  309. }
  310. }
  311. $v['i_extend'] = str_replace("\n", "\n", $v['i_extend']);
  312. // return json($v['i_extend']);
  313. $price = Db::name('activity_price')->where('id',$item['price_id'])->find();
  314. $v['ladder_title'] = $price['ladder_title'];
  315. if($v['pay_type'] == 1 || $v['pay_type'] == 2){
  316. $v['pay_type_text'] = 'H5';
  317. }else if($v['pay_type'] == 3 || $v['pay_type'] == 4){
  318. $v['pay_type_text'] = 'APP';
  319. }else if($v['pay_type'] == 5){
  320. $v['pay_type_text'] = '公众号';
  321. }else if($v['pay_type'] == 6 || $v['pay_type'] == 7){
  322. $v['pay_type_text'] = 'PC';
  323. }else{
  324. $v['pay_type_text'] = '未支付';
  325. }
  326. $v['is_sh'] = $price['is_sh'] == 1 ? '需要审核':'不需要审核';
  327. $v['is_hx'] = $item['is_hx'] == 1 ? '已核销':'未核销';
  328. $all[] = $v;
  329. }
  330. }
  331. $field=array(
  332. 'A' => array('order_no', '订单号'),
  333. 'B' => array('user_name', '联系人'),
  334. 'C' => array('phone', '电话'),
  335. 'D' => array('money','订单金额'),
  336. 'E' => array('num','人数'),
  337. 'F' => array('act_title','活动'),
  338. 'G' => array('order_source', '订单类型'),
  339. 'H' => array('pay_desc', '支付状态'),
  340. 'I' => array('tg_phone', '推广手机号'),
  341. 'J' => array('qrcode', '推广码链接'),
  342. 'K' => array('i_name', '姓名'),
  343. 'L' => array('i_phone', '手机号'),
  344. 'M' => array('i_email', '邮箱'),
  345. 'N' => array('ladder_title', '购票类型'),
  346. 'O' => array('is_sh', '是否审核'),
  347. 'P' => array('pay_type_text', '支付场景'),
  348. 'Q' => array('is_hx', '核销状态'),
  349. 'R' => array('i_extend', '答题模板'),
  350. );
  351. // return json($data);
  352. $this->phpExcelList($field,$all,'报名订单_'.date('Y-m-d H:i:s'));
  353. }
  354. public function phpExcelList($field=[],$list=[],$title='文件'){
  355. $PHPExcel=new \PHPExcel();
  356. $PHPSheet=$PHPExcel->getActiveSheet();
  357. $PHPSheet->setTitle('demo'); //给当前活动sheet设置名称
  358. foreach($list as $key=>$value)
  359. {
  360. foreach($field as $k=>$v){
  361. if($key == 0){
  362. $PHPSheet= $PHPExcel->getActiveSheet()->setCellValue($k.'1',$v[1]);
  363. }
  364. $i=$key+2;
  365. $PHPExcel->getActiveSheet()->setCellValue($k . $i, $value[$v[0]]);
  366. }
  367. }
  368. $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,'Excel2007'); //按照指定格式生成Excel文件,
  369. header('Content-Type: application/vnd.ms-excel'); // 告诉浏览器生成一个excel05版的表格
  370. header("Content-Disposition: attachment;filename={$title}.xls"); //告诉浏览器输出文件的名称
  371. header('Cache-Control: max-age=0'); //禁止缓存
  372. $PHPWriter->save("php://output"); //输出到浏览器
  373. }
  374. /**
  375. * 重新提醒
  376. * @auth true
  377. * @throws \think\Exception
  378. * @throws \think\exception\PDOException
  379. */
  380. public function remind(){
  381. $order = \app\common\model\ActivityApplyItem::where('id','in',input('id'))->group('apply_id')->select()->toArray();
  382. $ret_val = true;$msg='';
  383. Db::startTrans();
  384. try{
  385. foreach ($order as $k => $v){
  386. $order_info = \app\common\model\ActivityApply::where('id',$v['apply_id'])->find();
  387. // 更改订单状态
  388. //\app\common\model\ActivityApply::where('id',$order_info['id'])->update(['status'=>1,'pay_state'=>1,'pay_at'=>date('Y-m-d H:i:s')]);
  389. $item_list = \app\common\model\ActivityApplyItem::where('apply_id',$order_info['id'])->select()->toArray();
  390. $qrCode = new \Endroid\QrCode\QrCode();
  391. foreach ($item_list as $iv) {
  392. if($iv['sh_status'] == 1) {
  393. // 生成核销码
  394. $qr_content = $order_info['act_id'].'-'.$order_info['id'].'-'.$iv['id'];
  395. $qrCode->setText($qr_content);
  396. $qrCode->setSize(200);
  397. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/code';
  398. $filename = $dir.'/'.$iv['id'].'.png';
  399. $qrCode->writeFile($filename);
  400. // return json($order_info);
  401. $url='http://'.$_SERVER['SERVER_NAME']."/code/".$iv['id'].'.png';
  402. \app\common\model\ActivityApplyItem::where('id',$iv['id'])->update(['status'=>1,'qr_code'=>$url,'qr_content'=>$qr_content]);
  403. $act_title = \app\common\model\Activity::where('id',$iv['act_id'])->value('title');
  404. $content = '您已成功报名"'.$act_title.'"';
  405. UserMessage::sendUserMessage($iv['user_id'],'apply',1,0,0,$iv['id'],$content,$iv['id']);
  406. UserSynth::ticketSend($iv['id']);
  407. }else{
  408. \app\common\model\ActivityApplyItem::where('id',$iv['id'])->update(['status'=>1]);
  409. }
  410. }
  411. // 活动订单发票记录修改
  412. BillApply::where(['order_type'=>3,'order_id'=>$order_info['id']])->update(['order_pay'=>1]);
  413. }
  414. //UserMessage::sendUserMessage($order_info['user_id'],'activity',1,0,0,$order_info['id'],'',$order_info['id']);
  415. Db::commit();
  416. }catch (\Exception $e){
  417. $ret_val = false;
  418. $msg = $e->getMessage();
  419. Db::rollback();
  420. }
  421. $this->success('操作成功');
  422. }
  423. }