Activity.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace app\adminall\controller;
  3. use think\facade\View;
  4. use think\facade\Request;
  5. use app\logics\Activity as Logic;
  6. use app\model\Activity as ModelActivity;
  7. use app\model\ActivityCustomerExportLog;
  8. use app\model\Footprints;
  9. use think\facade\Db;
  10. use app\model\Employee;
  11. use app\model\Customer;
  12. use app\model\Org;
  13. use app\model\Permission;
  14. use app\model\Grant;
  15. use app\model\CustomerVisitLog;
  16. use app\model\ActivityMaterial;
  17. use app\model\ActivityCollor;
  18. class Activity
  19. {
  20. private $root_id = 0;//总后台店面id默认0
  21. private $employee_id = 0;
  22. /**
  23. * 列表
  24. */
  25. public function list()
  26. {
  27. if (!Request::isAjax()) {
  28. $eid = ModelActivity::where([['del', '=', 0], ['root_id', '=', $this->root_id], ['employee_id', '>', 0]])->group('employee_id')->column('employee_id');
  29. $where = [
  30. ['id', 'in', $eid],
  31. ['grant_id', '>', 0],
  32. ['root_id', '=', $this->root_id]
  33. ];
  34. // 运营人员列表
  35. $employee = Employee::where($where)->field(['id', 'opt_name as name'])->select()->toArray();
  36. View::assign('employee', $employee);
  37. return View::fetch();
  38. }
  39. $param = Request::only(['page', 'limit', 'keyword', 'date', 'employee_id']);
  40. $where = [
  41. ['del', '=', 0],
  42. ['root_id', '=', $this->root_id]
  43. ];
  44. if (!empty($param['date'])) {
  45. $dates = explode(' - ', $param['date']);
  46. $where[] = ['start_date', '<=', "$dates[0]"];
  47. $where[] = ['end_date', '>=', "$dates[1]"];
  48. }
  49. if (!empty($param['keyword'])) {
  50. $where[] = ['title', 'like', '%' . $param['keyword'] . '%'];
  51. }
  52. if (!empty($param['employee_id'])) {
  53. $where[] = ['employee_id', '=', $param['employee_id']];
  54. }
  55. $list = ModelActivity::with(['employee'])->where($where)->page($param['page'], $param['limit'])->field('*,end_date timestate')->order('addtime desc')->select()->toArray();
  56. $aids = [];
  57. foreach ($list as &$val) {
  58. $val['arr_qrcode_employee_id'] = [];
  59. if (!empty($val['qrcode_employee_id'])) {
  60. $allid = explode(',', $val['qrcode_employee_id']);
  61. $val['arr_qrcode_employee_id'] = $allid;
  62. foreach ($allid as $k => $v) {
  63. $aids[] = $v;
  64. }
  65. }
  66. }
  67. $qrcode = Employee::where([['id', 'in', array_unique($aids)]])->column('name', 'id');
  68. //$qrcode = Employee::where([['id', 'in', array_column($list, 'qrcode_employee_id')]])->column('name', 'id');
  69. foreach ($list as $k => $v) {
  70. $qrcode_name = '';
  71. foreach ($qrcode as $r => $e) {
  72. if (in_array($r, $v['arr_qrcode_employee_id'])) {
  73. $qrcode_name .= $e . ',';
  74. }
  75. }
  76. $list[$k]['qrcode_name'] = isset($qrcode_name) ? trim($qrcode_name, ',') : '';
  77. }
  78. $count = ModelActivity::where($where)->count();
  79. //邀约客户
  80. if ($list) {
  81. $aids = array_column($list, 'id');
  82. $v_where[] = ['state', 'in', CustomerVisitLog::changeState('预约活动', 'chaos')];
  83. $v_where[] = ['aid', 'in', $aids];
  84. $footprints = CustomerVisitLog::withJoin('customer')->where([
  85. ['customer_visit_log.state', 'in', CustomerVisitLog::changeState('预约活动', 'chaos')],
  86. ['customer_visit_log.aid', 'in', $aids]
  87. ])->group('customer_visit_log.aid,customer_visit_log.customer_id')->column('customer_visit_log.aid as aid,customer_visit_log.customer_id as customer_id');
  88. $arr = [];
  89. foreach ($footprints as $k => $v) {
  90. $arr[$v['aid']] = isset($arr[$v['aid']]) ? $arr[$v['aid']] += 1 : 1;
  91. }
  92. $group = ActivityCollor::where([['aid','in',array_column($list,'id')],['type','=',1]])->group('aid')->column('count(*)','aid');
  93. $collor = ActivityCollor::where([['aid','in',array_column($list,'id')],['type','=',0]])->group('aid')->column('count(*)','aid');
  94. $material = ActivityMaterial::where([['aid','in',array_column($list,'id')]])->group('aid')->column('count(*)','aid');
  95. foreach ($list as $k => $v) {
  96. $list[$k]['user_count'] = isset($arr[$v['id']]) ? $arr[$v['id']] : 0;
  97. $list[$k]['start_date'] = str_replace('-','/',$v['start_date']);
  98. $list[$k]['end_date'] = str_replace('-','/',$v['end_date']);
  99. $list[$k]['activity_sign_count'] = isset($group[$v['id']]) ? $group[$v['id']] : 0;
  100. $list[$k]['collect_count'] = isset($collor[$v['id']]) ? $collor[$v['id']] : 0;
  101. $list[$k]['material_count'] = isset($material[$v['id']]) ? $material[$v['id']] : 0;
  102. }
  103. }
  104. return json(['code' => 0, 'data' => $list, 'count' => $count, 'msg' => '获取成功']);
  105. }
  106. /**
  107. * 添加视图
  108. */
  109. public function add()
  110. {
  111. if (request()->isAjax()) return $this->addActivity();
  112. $root_id = $this->root_id;
  113. //扫码人员
  114. $employee = Employee::where([['uid', '>', 0], ['state', '=', '在职'], ['root_id', '=', $root_id]])->column('id,name');
  115. View::assign('employee', $employee);
  116. return View::fetch();
  117. }
  118. /**
  119. * 添加活动
  120. */
  121. public function addActivity()
  122. {
  123. $data = Request::only(['title', 'content', 'type', 'cover', 'money', 'start_date', 'end_date', 'wxmch', 'cate', 'poster', 'address','qrcode','introduce']);
  124. $data['root_id'] = $this->root_id;
  125. $data['employee_id'] = $this->employee_id;
  126. if ($data['start_date'] > $data['end_date']) return json(['code' => 1, 'msg' => '开始日期需在结束日期之前']);
  127. ModelActivity::insert($data);
  128. return json(['code' => 0, 'msg' => '添加成功']);
  129. }
  130. /**
  131. * 编辑
  132. */
  133. public function editActivity()
  134. {
  135. $data = Request::only(['title', 'content', 'type', 'cover', 'money', 'start_date', 'end_date', 'wxmch', 'cate', 'poster', 'address','qrcode','introduce']);
  136. $id = input('id',0);
  137. $info = ModelActivity::where([['id','=',$id]])->findOrEmpty();
  138. if($info->isEmpty()) return json(['code' => 1, 'msg' => '编辑失败','data'=>'编辑失败']);
  139. foreach ($data as $key => $value) {
  140. $info->$key = $value;
  141. }
  142. $info->save();
  143. return json(['code' => 0, 'msg' => '添加成功']);
  144. }
  145. /**
  146. * 修改视图
  147. */
  148. public function edit($id)
  149. {
  150. $edit = (new ModelActivity())->where([
  151. 'id' => $id, 'root_id' => $this->root_id
  152. ])->find();
  153. $edit->coverc = $edit->getData('cover');
  154. $edit->posterc = $edit->getData('poster');
  155. View::assign('data', $edit);
  156. $root_id = $this->root_id;
  157. //扫码人员
  158. $employee = Employee::where([['uid', '>', 0], ['state', '=', '在职'], ['root_id', '=', $root_id]])->column('id,name');
  159. View::assign('employee', $employee);
  160. return View::fetch();
  161. }
  162. /*
  163. * 富文本图片
  164. */
  165. public function imgUpload()
  166. {
  167. $bindUrl = config('app.ali_oss_bindurl');
  168. $data = [
  169. 'src' => 'http://' . $bindUrl . '/' . Request::param('file'),
  170. 'title' => ''
  171. ];
  172. return json(['code' => 0, 'msg' => '', 'data' => $data]);
  173. }
  174. /*
  175. * 上传素材
  176. */
  177. public function upload_material($id)
  178. {
  179. View::assign('id', $id);
  180. return View::fetch();
  181. }
  182. /*
  183. * 上传素材
  184. */
  185. public function source_material($id)
  186. {
  187. View::assign('id', $id);
  188. return View::fetch();
  189. }
  190. /*
  191. * 素材列表
  192. */
  193. public function materialList($id)
  194. {
  195. $data = Request::only(['page'=>0,'limit'=>10]);
  196. $list = ActivityMaterial::where([['aid','=',$id],['root_id','=',0]])->order('id desc')->page((int)$data['page'],(int)$data['limit'])->select()->toArray();
  197. $count = ActivityMaterial::where([['aid','=',$id],['root_id','=',0]])->count();
  198. return json(['code' => 0, 'msg' => '', 'data' => $list,'count'=>$count]);
  199. }
  200. /*
  201. * 删除素材
  202. */
  203. public function materialDel($id)
  204. {
  205. ActivityMaterial::where([['id','=',$id],['root_id','=',0]])->delete();
  206. return json(['code' => 0, 'msg' => '删除成功', 'data' =>'删除成功']);
  207. }
  208. /*
  209. * 上传素材
  210. * type=0图 1视频
  211. */
  212. public function materialAdd($id)
  213. {
  214. $data = Request::only(['type'=>0,'urls'=>'']);
  215. $save = [];
  216. $arr = $data['urls'] ? explode(',',$data['urls']) : [];
  217. foreach ($arr as $value) {
  218. $save[] = [
  219. 'aid'=>$id,
  220. 'type'=>$data['type'],
  221. 'url'=>$value,
  222. 'root_id'=>0
  223. ];
  224. }
  225. ActivityMaterial::insertAll($save);
  226. return json(['code' => 0, 'msg' => '保存成功', 'data' =>'保存成功']);
  227. }
  228. }