LiveAppointment.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace app\store\controller;
  3. use library\Controller;
  4. use think\Db;
  5. /**
  6. * 直播预约
  7. * Class LiveAppointment
  8. * @package app\store\controller
  9. */
  10. class LiveAppointment extends Controller
  11. {
  12. /**
  13. * 绑定数据表
  14. * @var string
  15. */
  16. protected $table = 'LiveGoodsApp';
  17. /**
  18. * 预约列表
  19. * @auth true
  20. * @menu true
  21. * @throws \think\Exception
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. * @throws \think\exception\DbException
  25. * @throws \think\exception\PDOException
  26. */
  27. public function index()
  28. {
  29. $this->title = '预约列表';
  30. $live_id = input('id');
  31. $goods_id = input('goods_id',0);
  32. $where= [];
  33. if($this->request->request('live_name')) $where[]= ['l.name','=',$this->request->request('live_name')];
  34. if($this->request->request('live_time')){
  35. $live_time = $this->request->request('live_time');
  36. $where[]= ['start_at','between time',[$live_time.' 00:00:00',$live_time.' 23:59:59']];
  37. }
  38. if($live_id) $where[] = ['live_id','=',$live_id];
  39. if($goods_id) $where[] = ['goods_id','=',$goods_id];
  40. $this->goods_id = $goods_id;
  41. $this->live_id = $live_id;
  42. $this->live_status = Db::table('store_live')->where('id',$live_id)->value('status');
  43. // 预约总数量
  44. $app_nums = Db::table('live_goods_app')->where(['live_id'=>$live_id])->count();
  45. //直播商品
  46. $all_goods = Db::table('store_live_goods')
  47. ->alias('l')
  48. ->field('l.goods_id,g.name')
  49. ->join('live_goods_list g',' g.id = l.goods_id ','LEFT')
  50. ->order('l.sort desc ,l.id desc')
  51. ->where(['live_id'=>$live_id])
  52. ->select();
  53. $live_goods = ['0'=>'全部'];
  54. array_map(function ($v)use (&$live_goods,$live_id,$app_nums){
  55. $goods_app = Db::table('live_goods_app')
  56. ->where(['live_id'=>$live_id,'goods_id'=>$v['goods_id']])
  57. ->count();
  58. $rete = $app_nums > 0 && $goods_app > 0 ? bcdiv($goods_app,$app_nums,2) * 100 : 0;
  59. $live_goods[$v['goods_id']] = $v['name']. ' ---- ' . $rete .'%';
  60. },$all_goods);
  61. $this->live_goods = $live_goods;
  62. $query = $this->_query($this->table)->alias('a')
  63. ->field('a.*,m.name as user_name,l.name as live_name,g.name as goods_name,g.cover,l.start_at')
  64. ->join('store_live l',' l.id = a.live_id ','LEFT')
  65. ->join('store_member m',' m.id = a.user_id ','LEFT')
  66. ->join('live_goods_list g',' g.id = a.goods_id ','LEFT')
  67. ->like('a.name');
  68. if(!empty($where)) $query->where($where);
  69. $query->order('a.id desc')->page();
  70. }
  71. /**
  72. * 数据列表处理
  73. * @auth true
  74. * @menu true
  75. * @param array $data
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. * @throws \think\exception\DbException
  79. */
  80. protected function _index_page_filter(&$data)
  81. {
  82. }
  83. public function send_msg()
  84. {
  85. $live_id = input('live_id');
  86. $goods_id = input('goods_id');
  87. $user_app = Db::table('live_goods_app')
  88. ->alias('a')
  89. ->field('a.id,a.user_id, m.openid,m.name')
  90. ->join('store_member m' , 'a.user_id = m.id','LEFT')
  91. ->where(['a.live_id'=>$live_id,'a.goods_id'=>$goods_id,'is_remind'=>0])
  92. ->select();
  93. if(empty($user_app)){
  94. echo json_encode(['code'=>201,'msg'=>'暂无需要提醒的预约!']);die();
  95. }
  96. $good_info = Db::table('live_goods_list')->find($goods_id);
  97. $st_time = Db::table('store_live')->where('id',$live_id)->value('start_at');
  98. // 发消息
  99. $jump_url = Db::table('platform_set')->where(['name'=>'push_link'])->value('content');
  100. $access_token = get_access_token();
  101. foreach ($user_app as $uv) {
  102. $data=[
  103. '姐妹们,你预约的商品马上开播咯!',
  104. $uv['name'],
  105. $good_info['name'],
  106. $st_time,
  107. '快来【樊樊直播间】抢购吧!Get超多直播福利!',
  108. ];
  109. $send_res = send_message($access_token,$uv['openid'],$jump_url,$data);
  110. }
  111. // 更新提醒状态
  112. Db::table('live_goods_app')->where(['live_id'=>$live_id,'goods_id'=>$goods_id,'is_remind'=>0])->update(['is_remind'=>1]);
  113. echo json_encode(['code'=>200,'msg'=>'发送成功']);
  114. }
  115. }