1
0

Spellgroup.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\sys\controller;
  3. use think\facade\View;
  4. use think\facade\Request;
  5. use app\model\Spellgroup as ModelSpellgroup;
  6. use app\model\SpellgroupJoin;
  7. use app\model\SpellgroupTeam;
  8. use app\model\Employee;
  9. class Spellgroup
  10. {
  11. /**
  12. * 拼团列表
  13. */
  14. public function list()
  15. {
  16. if (!Request::isAjax()) {
  17. return View::fetch();
  18. }
  19. $param = Request::only(['page', 'limit', 'keyword']);
  20. $where[] = ['del', '=', 0];
  21. if (!empty($param['keyword'])) {
  22. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  23. }
  24. $list = ModelSpellgroup::where($where)->page($param['page'] , $param['limit'])->order('addtime desc')->select()->toArray();
  25. $column = array_column($list , 'id');
  26. $tid = SpellgroupTeam::where('spellgroup_id','in',$column)->group('spellgroup_id')->column('count(spellgroup_id)','spellgroup_id');
  27. $uid = SpellgroupJoin::where('spellgroup_id','in',$column)->group('spellgroup_id')->column('count(spellgroup_id)','spellgroup_id');
  28. foreach($list as &$item){
  29. $item['end'] = $item['start_date'] <= date('Y-m-d') ? 1 : 0;
  30. $item['team_num'] = isset($tid[$item['id']]) ? $tid[$item['id']] : 0;
  31. $item['join_user_num'] = isset($uid[$item['id']]) ? $uid[$item['id']] : 0;
  32. }
  33. $count = ModelSpellgroup::where($where)->count();
  34. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  35. }
  36. //团队列表
  37. public function team_list()
  38. {
  39. $request = request();
  40. $param = Request::only(['page', 'limit', 'keyword','team_num','status']);
  41. $where[] = ['root_id', '=', $request->employee->root_id];
  42. if (!empty($param['status'])) {
  43. $where[] = ['status', '=', $param['status']];
  44. }
  45. if (!empty($param['keyword'])) {
  46. $eids = Employee::where([['root_id','=',$request->employee->root_id],['state','=','在职'],['uid','>',0],['name','like','%'.trim($param['keyword']).'%']])->column('id');
  47. $where[] = ['employee_id','in',$eids];
  48. }
  49. if (!empty($param['team_num'])) {
  50. $where[] = ['number', '=', $param['team_num']];
  51. }
  52. $list= SpellgroupTeam::with(['user'=>function($query){
  53. $query->field('id,nickname,phone');
  54. },'employee'=>function($query){
  55. $query->field('id,name');
  56. }])
  57. ->where($where)
  58. ->page($param['page'] , $param['limit'])
  59. ->order('open_time desc')
  60. ->select()
  61. ->toArray();
  62. foreach($list as $key=>$val){
  63. $list[$key]['end_time']=date('Y-m-d H:i:s',$val['end_time']);
  64. }
  65. $count = SpellgroupTeam::where($where)->count();
  66. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  67. }
  68. //查看团队成员
  69. public function sel_team_detail()
  70. {
  71. $request = request();
  72. $param = Request::only(['id']);
  73. $team_data=SpellgroupTeam::where('id',$param['id'])->find();
  74. $join_list=SpellgroupJoin::with(['user'])
  75. ->where('team_id',$param['id'])
  76. ->field('join_uid,addtime,is_heads')
  77. ->order('is_heads desc addtime asc')
  78. ->select()
  79. ->ToArray();
  80. View::assign('list', $join_list);
  81. View::assign('team_data', $team_data);
  82. return View::fetch();
  83. //return json(['code' => 0, 'data' => $team_data, 'msg' => '获取成功']);
  84. }
  85. /**
  86. * 添加视图
  87. */
  88. public function add()
  89. {
  90. $request = request();
  91. $group_data=ModelSpellgroup::where('root_id',$request->employee->root_id)->find();
  92. View::assign('data', $group_data);
  93. return View::fetch();
  94. }
  95. //设置拼团
  96. public function set()
  97. {
  98. $request = request();
  99. $group_data=ModelSpellgroup::where([['root_id','=',$request->employee->root_id]])->findOrEmpty();
  100. $group_data->team_ok_type=!empty($group_data->team_ok_type)?$group_data->team_ok_type:'队员交定后';
  101. View::assign('data', $group_data);
  102. return View::fetch();
  103. }
  104. /**
  105. * 添加活动
  106. */
  107. public function addSave()
  108. {
  109. $request = request();
  110. $data = Request::only(['id', 'content', 'hour', 'cover', 'number','heads_reward_type','heads_reward_cont1','heads_reward_cont2','team_member_cont','team_ok_type']);
  111. //var_dump($data);
  112. //exit;
  113. if($data['heads_reward_type']==1){
  114. $data['heads_reward_cont']=$data['heads_reward_cont1'];
  115. }
  116. if($data['heads_reward_type']==2){
  117. $data['heads_reward_cont']=$data['heads_reward_cont2'];
  118. }
  119. $group_data=ModelSpellgroup::where('root_id',$request->employee->root_id)->find();
  120. if(empty($group_data)){
  121. $data['root_id']=$request->employee->root_id;
  122. (new ModelSpellgroup())->save($data);
  123. return json(['code' => 0, 'msg' => '添加成功']);
  124. }else{
  125. $ms=$group_data->save($data);
  126. if($ms){
  127. return json(['code' => 0, 'msg' => '修改成功']);
  128. }else{
  129. return json(['code' => 1, 'msg' => '修改失败']);
  130. }
  131. }
  132. }
  133. /**
  134. * 修改视图
  135. */
  136. public function edit($id)
  137. {
  138. $edit = (new ModelSpellgroup())->find($id);
  139. View::assign('data', $edit);
  140. return View::fetch();
  141. }
  142. /**
  143. * 修改保存活动
  144. */
  145. public function editSave()
  146. {
  147. $data = Request::only(['id', 'title', 'content', 'hour', 'cover', 'number']);
  148. $obj = (new ModelSpellgroup())->find($data['id']);
  149. if (empty($obj)) return json(['code' => 1, 'msg' => '数据不存在']);
  150. if ($obj['start_date'] <= date('Y-m-d')) return json(['code' => 1, 'msg' => '活动已开始,不支持编辑']);
  151. //更新队伍记录的拼团人数
  152. if($obj['number'] != $data['number'])
  153. SpellgroupTeam::where('spellgroup_id',$data['id'])->update(['number'=>$data['number']]);
  154. $obj->save($data);
  155. return json(['code' => 0, 'msg' => '修改成功']);
  156. }
  157. /**
  158. * 活动领取列表
  159. */
  160. public function receive($id , $tid = 0)
  161. {
  162. if (!Request::isAjax()) {
  163. View::assign('id',$id);
  164. View::assign('tid',$tid);
  165. return View::fetch();
  166. }
  167. $param = Request::only(['id','page', 'limit', 'keyword' , 'tid']);
  168. $where[] = ['spellgroup_id','=',$id];
  169. if(!empty($param['keyword']))
  170. {
  171. $where[] = ['phone', 'like', '%' . $param['keyword'] . '%'];
  172. }
  173. if(!empty($param['tid']))
  174. {
  175. $where[] = ['team_id', '=', $param['tid']];
  176. }
  177. $data = SpellgroupJoin::with(['user'])->where($where)->page($param['page'] , $param['limit'])->field('id,join_uid,phone,status')->order('addtime desc')->select();
  178. $count = SpellgroupJoin::where($where)->count();
  179. return json(['code' => 0 , 'data'=>$data , 'count'=>$count , 'msg' => '获取成功']);
  180. }
  181. /*
  182. * 团队列表
  183. */
  184. public function team($id,$hour)
  185. {
  186. if (!Request::isAjax()) {
  187. View::assign('id',$id);
  188. View::assign('hour',$hour);
  189. return View::fetch();
  190. }
  191. $param = Request::only(['id','hour', 'page', 'limit']);
  192. $data = SpellgroupTeam::where('spellgroup_id',$param['id'])->page($param['page'] , $param['limit'])->order('open_time desc')->select()->toArray();
  193. $tid = array_column($data,'id');
  194. $user = SpellgroupJoin::with(['user'])->where('team_id','in',$tid)->field('join_uid,team_id')->select()->toArray();
  195. foreach($data as &$item)
  196. {
  197. $item['end'] = date('Y-m-d h:i:s', strtotime($item['open_time'])+($param['hour']*3600));
  198. $item['headimgurl'] = [];
  199. foreach($user as $v)
  200. {
  201. if($item['id'] == $v['team_id']) $item['headimgurl'][] = $v['headimgurl'];
  202. }
  203. }
  204. $count = SpellgroupTeam::where('spellgroup_id',$param['id'])->count();
  205. return json(['code' => 0 , 'data'=>$data , 'count'=>$count , 'msg' => '获取成功']);
  206. }
  207. /*
  208. * 富文本图片
  209. */
  210. public function imgUpload()
  211. {
  212. $bindUrl = config('app.ali_oss_bindurl');
  213. $data = [
  214. 'src' => 'http://' . $bindUrl . '/' . Request::param('file'),
  215. 'title' => ''
  216. ];
  217. return json(['code' => 0, 'msg' => '', 'data' => $data]);
  218. }
  219. }