123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567 |
- <?php
- namespace app\api\controller;
- use app\model\CustomerClue;
- use app\model\Employee;
- use app\model\MaterialCase;
- use app\model\Community;
- use app\model\Decostyle;
- use app\model\Housetype;
- use app\model\EvidenceCate;
- use app\model\MaterialEvidence;
- use app\model\Miniprogram;
- use app\model\Org;
- use app\model\UserCollect;
- use think\facade\Console;
- use wx\miniprogram\Qrcode;
- use app\model\Designer;
- use app\model\User;
- class Material extends Base
- {
- /**
- * 装修案例风格
- */
- public function decostylelist()
- {
- $styleArr = Decostyle::where(['root_id' => request()->token['root_org']])->where([['type', '=', 0]])->field('id, name')->select();
- return json(['code' => 0, 'data' => $styleArr, 'msg' => '获取成功']);
- }
- /**
- * 装修案例小区列表
- */
- public function communitylist()
- {
- $name = input('name', '', 'trim');
- if ($name) {
- $where[] = ['name', 'like', '%' . $name . '%'];
- }
- $from = input('from', '', 'trim');
- $where[] = ['type', '=', 0];
- $where[] = ['root_id', '=', request()->token['root_org']];
- $communityArr = Community::where($where)->field('id,name,pinyin,case_num')->order('pinyin asc')->select()->toArray();
- $comidlist = array_column($communityArr, 'id');
- $caseData = MaterialCase::where([['community_id', 'in', $comidlist], ['del', '=', 0], ['publish', '=', 1]])->group('community_id')->order('updatetime asc')->column('max(updatetime) as updatetime, sum(shared_times) as shared_times, count(id) as case_num', 'community_id');
- // 个人上传装修案例,数量显示为此小区下此员工上传的数量
- if ($from) {
- foreach ($caseData as $k => $v) {
- $caseData[$k]['case_num'] = MaterialCase::where([['community_id', '=', $v['community_id']], ['from', '=', 1], ['employee_id', '=', request()->token['employee_id']]])->count();
- }
- }
- foreach ($communityArr as &$i) {
- if (isset($caseData[$i['id']])) {
- $i['updatetime'] = $caseData[$i['id']]['updatetime'];
- $i['shared_times'] = (int)$caseData[$i['id']]['shared_times'];
- $i['case_num'] = (int)$caseData[$i['id']]['case_num'];
- } else {
- $i['updatetime'] = 0;
- $i['shared_times'] = 0;
- $i['case_num'] = 0;
- }
- }
- return json(['code' => 0, 'data' => $communityArr, 'msg' => '获取成功']);
- }
- /*
- * 装修案例户型
- */
- public function housetypelist()
- {
- $housetypeArr = Housetype::where(['root_id' => request()->token['root_org']])->field('id,name')->select();
- return json(['code' => 0, 'data' => $housetypeArr, 'msg' => '获取成功']);
- }
- /*
- * material case list
- */
- public function caselist()
- {
- $param = $this->request->param();
- $order = (!isset($param['order']) || !$param['order']) ? 'addtime' : $param['order'];
- $desc = (!isset($param['desc']) || !$param['desc']) ? 'desc' : $param['desc'];
- // $order = $order . ' ' . $desc;
- $where[] = ['root_id', '=', request()->token['root_org']];
- // 我上传的
- if (!empty($param['my_case'])) {
- switch ($param['my_case']) {
- case 'publish': //通过的
- $where[] = ['publish', '=', 1];
- $where[] = ['del', '=', 0];
- $where[] = ['employee_id', '=', request()->token['employee_id']];
- $where[] = ['from', '=', 1];
- break;
- case 'all': //全部的
- $where[] = ['employee_id', '=', request()->token['employee_id']];
- $where[] = ['from', '=', 1];
- break;
- default:
- $where[] = ['publish', '=', 1];
- $where[] = ['del', '=', 0];
- $where[] = ['employee_id', '=', request()->token['employee_id']];
- $where[] = ['from', '=', 1];
- break;
- }
- } else {
- $where[] = ['del', '=', 0];
- $where[] = ['publish', '=', 1];
- }
- setCondition($param, ['commu_id', 'community_id'], '=', $where);
- setCondition($param, 'style_id', '=', $where);
- setCondition($param, ['square_start', 'square'], '>=', $where);
- setCondition($param, ['square_end', 'square'], '<', $where);
- setCondition($param, ['keyword', 'title'], ['like', '%VALUE%'], $where);
- setCondition($param, 'housetype_id', '=', $where);
- if (!empty($param['id'])) $where[] = ['designer_id', '=', $param['id']];
- if (!empty($param['case_type'])) {
- if ($param['case_type'] == 1) {
- $where[] = ['desc', '<>', ''];
- $where[] = ['desc', 'not null',''];
- }
- if ($param['case_type'] == 2) {
- $where[] = ['real_case', '<>', ''];
- $where[] = ['real_case', 'not null',''];
- }
- }
- $page = !empty($param['page']) ? $param['page'] : 1;
- $limit = !empty($param['limit']) ? $param['limit'] : 15;
- $list = MaterialCase::with(['designer' => function ($query) {
- $query->field('id,name, headimgurl,addtime');
- }, 'community' => function ($query) {
- $query->field('id,name');
- }, 'decostyle' => function ($query) {
- $query->field('id,name');
- }])->where($where);
- if ($order == 'clue_number' || empty($param['order'])) {
- $list = $list->order('recommend desc, addtime desc')->select()->toArray();
- } else {
- $list = $list->page($page, $limit)->order('recommend desc, ' . $order . ' ' . $desc)->select()->toArray();
- }
- $collectedIds = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'materialCase'])->column('content_id');
- $list_ids = array_column($list, 'id');
- $clue_number = CustomerClue::where([['pipe_type', '=', 'materialCase'], ['pipe_id', 'in', $list_ids]])->group('pipe_id')->column('count(id) as id_count', 'pipe_id');
- foreach ($list as &$item) {
- if (in_array($item['id'], $collectedIds)) {
- $item['collected'] = true;
- } else {
- $item['collected'] = false;
- }
- $item['clue_number'] = isset($clue_number[$item['id']]) ? $clue_number[$item['id']] : 0;
- /** -----乐尚企业演示,线索数量进行调整按照分享量的三分之一展示,正式使用系统时删除此代码 ------*/
- if(request()->token['root_org'] == 1793) {
- $item['clue_number'] = floor($item['shared_times'] > 0 ? $item['shared_times']/3 : 0);
- }
- /*------------------------------------------------------------------------*/
- }
- // 排序
- if ($order == 'clue_number') {
- $sort = $desc == 'desc' ? SORT_DESC : SORT_ASC;
- array_multisort(array_column($list, $order), $sort, array_column($list, 'addtime'), SORT_DESC, $list);
- $list = array_slice($list, ($page-1) * $limit, $limit);
- }
- // 默认排序 vr&video->vr->video->article
- if (empty($param['order'])){
- $new_list['recommend'] = [];
- $new_list['vr_video'] = [];
- $new_list['vr'] = [];
- $new_list['video'] = [];
- $new_list['article'] = [];
- foreach ($list as $k => $v){
- if ($v['recommend'] == 1) {
- $new_list['recommend'][] = $v;
- }elseif (!empty($v['vr_case']) && !empty($v['video_case'])){
- $new_list['vr_video'][] = $v;
- } elseif (!empty($v['vr_case'])){
- $new_list['vr'][] = $v;
- } elseif (!empty($v['video_case'])){
- $new_list['video'][] = $v;
- } else {
- $new_list['article'][] = $v;
- }
- }
- $all_list = [];
- foreach ($new_list as $k => $v){
- foreach ($v as $vv){
- $all_list[] = $vv;
- }
- }
- $list = array_slice($all_list, ($page-1) * $limit, $limit);
- }
- return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']);
- }
- /**
- * 装修案例
- */
- public function casedetail()
- {
- $id = $this->request->param('id');
- $token = $this->request->token;
- $colected = UserCollect::where(['user_id' => $token['uid'], 'content_type' => 'materialCase', 'content_id' => $id])->count();
- $data = MaterialCase::with([
- 'community',
- 'designer',
- 'decostyle',
- 'housetype'
- ])->where(['id' => $id, 'root_id' => $token['root_org']])->find();
- //MaterialCase::where(['id' => $id, 'root_id' => $token['root_org']])->inc('view_times')->update();
- $data['collected'] = $colected > 0 ? true : false;
- // 关联案例
- if (!empty($data['designer_id'])){
- $relatedcaselist = MaterialCase::where([['designer_id', '=', $data['designer_id']],['id', '<>', $id], ['root_id', '=', $token['root_org']], ['del', '=', 0], ['publish', '=', 1]])
- ->with(['community' => function ($query) {
- $query->field('id,name');
- }, 'decostyle' => function ($query) {
- $query->field('id,name');
- }])->page(1, 4)->order('id desc')->select()->each(function ($item) {
- $item->clue_number = CustomerClue::where([['pipe_type', '=', 'materialCase'], ['pipe_id', '=', $item['id']]])->count();
- })->toArray();
- } else {
- $relatedcaselist = [];
- }
- $data['designer_related_cases'] = $relatedcaselist;
- //设计师附属字段
- if ($data->designer) {
- $designer = Designer::where([['employee_id', '=', $data->designer_id], ['root_id', '=', $token['root_org']]])->field('id,good_at,position')->findOrEmpty();
- if ($designer->isEmpty()) {
- $data->designer->good_at = '';
- $data->designer->position = '';
- } else {
- $data->designer->good_at = !empty($designer->good_at) ? $designer->good_at : '';
- $data->designer->position = $designer->position ? $designer->position : '';
- }
- $data->designer->headimgurl = User::where('id', '=', $data->designer->uid)->value('headimgurl');
- }
- // 为null处理,防止前端报错
- $data['real_case'] = !is_null($data['real_case']) ? $data['real_case'] : '';
- $data['desc'] = !is_null($data['desc']) ? $data['desc'] : '';
- $data['vr_case'] = !is_null($data['vr_case']) ? $data['vr_case'] : '';
- $data['housetype']['name'] = $data->housetype->name;
- return json(['code' => 0, 'data' => $data, 'msg' => '获取成功']);
- }
- /**
- * 设计师相关案例
- */
- public function designercases()
- {
- $data = input();
- //$style_id = $data['style_id'];
- $page = $data['page'];
- $limit = $data['limit'];
- $material_case_id = input('material_case_id', '', 'intval');
- if ($material_case_id) {//相关推荐时,去除所查看的案例
- $where[] = ['id', '<>', $material_case_id];
- }
- //$where[] = ['style_id', '=', $style_id];
- $designer_id = input('designer_id', '', 'intval');
- if ($designer_id) {
- $where[] = ['designer_id', '=', $designer_id];
- } else {
- return json(['code' => 0, 'data' => [], 'msg' => '获取成功']);
- }
- $where[] = ['root_id', '=', request()->token['root_org']];
- $where[] = ['del', '=', 0];
- $where[] = ['publish', '=', 1];
- $relatedcaselist = MaterialCase::where($where)->with(['community', 'decostyle'])->page($page, $limit)->order('id desc')->select();
- return json(['code' => 0, 'data' => $relatedcaselist, 'msg' => '获取成功']);
- }
- /*
- * 口碑见证列表
- */
- public function evidencelist()
- {
- $param = $this->request->param();
- $param['difference'] = isset($param['difference']) ? $param['difference'] : 0;
- $order = (!isset($param['order']) || !$param['order']) ? 'addtime' : $param['order'];
- $desc = (!isset($param['desc']) || !$param['desc']) ? 'desc' : $param['desc'];
- $order = $order . ' ' . $desc;
- $where[] = ['root_id', '=', request()->token['root_org']];
- $where[] = ['del', '=', 0];
- $where[] = ['publish', '=', 1];
- if ($param['difference']) {
- $where[] = ['difference', '=', $param['difference']];
- }
- setCondition($param, 'cate', '=', $where);
- setCondition($param, ['keyword', 'title'], ['like', '%VALUE%'], $where);
- $page = !empty($param['page']) ? $param['page'] : 1;
- $limit = !empty($param['limit']) ? $param['limit'] : 15;
- $list = MaterialEvidence::where($where)->limit($limit)->page($page, $limit)->order($order)->select();
- $collectedIds = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'materialEvidence'])->column('content_id');
- foreach ($list as &$item) {
- if (in_array($item['id'], $collectedIds)) {
- $item['collected'] = true;
- } else {
- $item['collected'] = false;
- }
- }
- return json(['code' => 0, 'data' => $list, 'msg' => '获取成功']);
- }
- /*
- * 口碑见证详情
- */
- public function evidencedetail($id)
- {
- $evidence = MaterialEvidence::where(['id' => $id, 'root_id' => request()->token['root_org']])->find();
- //MaterialEvidence::where(['id' => $id, 'root_id' => request()->token['root_org']])->inc('view_times')->update();
- if (empty($evidence)) return json(['code' => self::error_msg, 'msg' => '数据不存在']);
- // 查询上一条
- $lastId = MaterialEvidence::where([['addtime', '>', $evidence->addtime], ['id', '<>', $id], ['root_id', '=', request()->token['root_org']], ['del', '=', 0], ['publish', '=', 1]])
- ->order('addtime asc')->value('id');
- $nextId = MaterialEvidence::where([['addtime', '<', $evidence->addtime], ['id', '<>', $id], ['root_id', '=', request()->token['root_org']], ['del', '=', 0], ['publish', '=', 1]])
- ->order('addtime desc')->value('id');
- $colected = UserCollect::where(['user_id' => $this->request->token['uid'], 'content_type' => 'materialEvidence', 'content_id' => $id])->count();
- $evidence['collected'] = $colected > 0 ? true : false;
- return json(['code' => 0, 'data' => $evidence, 'msg' => '获取成功', 'lastId' => $lastId, 'nextId' => $nextId]);
- }
- /**
- * 口碑见证分类
- */
- public function evidenceCate()
- {
- $styleArr = EvidenceCate::where(['root_id' => request()->token['root_org']])->field('id, name')->select();
- return json(['code' => 0, 'data' => $styleArr, 'msg' => '获取成功']);
- }
- /**
- * 收藏
- */
- public function collect()
- {
- $param = $this->request->param();
- $data = [
- 'user_id' => $this->request->token['uid'],
- 'content_type' => $param['content_type'],
- 'content_id' => $param['content_id']
- ];
- $collect = UserCollect::where($data)->find();
- if ($collect) {
- $collect->delete();
- return json(['code' => 0, 'msg' => '取消收藏']);
- } else {
- UserCollect::create($data);
- return json(['code' => 0, 'msg' => '收藏成功']);
- }
- }
- /**
- * 设计师
- */
- public function designer()
- {
- //设计师获取
- $where = [];
- $org_id = input('org_id', '', 'intval');
- if ($org_id) {
- $where[] = ['org_id', '=', $org_id];
- } else {
- $w[] = ['path', 'like', $this->request->token['root_org'] . '-%'];
- $w[] = ['org_type', '=', 2];
- $orgs = Org::where($w)->column('id');
- $where[] = ['org_id', 'in', $orgs];
- }
- $where[] = ['state', '=', '在职'];
- $list = Employee::where($where)->field('id,name,headimgurl')->select()->toArray();
- return json(['code' => self::success, 'data' => $list]);
- }
- public function designer_org()
- {
- //设计师部门获取
- $w[] = ['path', 'like', $this->request->token['root_org'] . '-%'];
- $w[] = ['org_type', '=', 2];
- $orgs = Org::where($w)->field(['id', 'name', 'pid'])->order('pid asc')->select()->toArray();
- foreach ($orgs as $k => $v) {
- $e_count = Employee::where([['org_id', '=', $v['id'], ['state', '=', '在职']]])->count();
- if ($e_count == 0) {
- unset($orgs[$k]);
- }
- }
- $orgs = array_values($orgs);
- return json(['code' => self::success, 'data' => $orgs]);
- }
- /**
- * 员工添加装修案例
- */
- public function case_add()
- {
- $param = $this->request->only(['title', 'community_name', 'community_id', 'designer_id', 'style_id', 'square', 'room', 'hall', 'toilet', 'cover_img', 'desc', 'housetype_id', 'money', 'img_content', 'cover_share_img', 'housetype_type'=>'housetype']);
- if (!empty($param['community_name'])) {
- //新增小区
- if (strlen($param['community_name']) > 45) {
- return json(['code' => self::error_msg, 'msg' => '小区名称过长']);
- }
- $condition[] = ['name', '=', $param['community_name']];
- $condition[] = ['type', '=', 0];
- $condition[] = ['root_id', '=', $this->request->token['root_org']];
- $exitedobj = Community::where($condition)->findorEmpty();
- if (!$exitedobj->isEmpty()) {
- $param['community_id'] = $exitedobj['id'];
- }else{
- $pinyin = hanzi2pinyin($param['community_name']);
- $community_id = Community::create([
- 'root_id' => $this->request->token['root_org'],
- 'pinyin' => $pinyin,
- 'name' => $param['community_name'],
- 'type' => 0,
- 'initials' => strtoupper(substr($pinyin, 0, 1)),
- 'employee_id'=> request()->token['employee_id']
- ]);
- $param['community_id'] = $community_id->id;
- }
- }
- if (!empty($param['img_content'])) {
- $img_content_data = [];
- if (isset($param['img_content'][0]['img'])) {
- foreach ($param['img_content'] as $k => $v) {
- $img_content_data[$k] = [
- 'img' => $v['img'],
- 'text' => $v['text']
- ];
- }
- }else{
- foreach ($param['img_content'] as $k => $v) {
- $img_content_data[$k] = [
- 'serverId' => $v['serverId'],
- 'text' => $v['text']
- ];
- }
- }
- $param['img_content'] = '';
- $param['img_download_status'] = 1;
- $param['img_content_data'] = json_encode($img_content_data);
- }
- $param['from'] = 1;
- $param['root_id'] = $this->request->token['root_org'];
- $param['employee_id'] = $this->request->token['employee_id'];
- $param['publish'] = 0;
- $result = MaterialCase::create($param);
- if ($result) {
- $rootOrgId = $this->request->token['root_org'];
- $mini = Miniprogram::where([['root_id', '=', $rootOrgId]])->find();
- $qrObj = new Qrcode;
- $mini_param = [
- 'scene' => 'cli=' . $mini['notify'] . '&cid=' . $result->id,
- 'page' => 'pages/decorateguidemsg/decorateguidemsg',
- 'width' => '280px',
- ];
- $qr = $qrObj->getUnlimited($mini->accesstoken, $mini_param);
- $rs = json_decode($qr, true);
- if (!is_null($rs)) {
- trace('装修案例二维码生成失败' . ';error:' . $qr, 'error');
- }
- $path = 'MaterialCaseQrcode/' . uniqid() . '.jpeg';
- ossContentUpload($path, $qr);
- MaterialCase::where('id', '=', $result->id)->save(['qrcode' => $path]);
- Console::call('download', ['material_case']);
- return json(['code' => self::success, 'msg' => '添加成功']);
- } else {
- return json(['code' => self::error_msg, 'msg' => '添加失败']);
- }
- }
- /**
- * 装修案例编辑
- */
- public function case_edit()
- {
- $param = $this->request->only(['id', 'title', 'community_name', 'community_id', 'designer_id', 'style_id', 'square', 'room', 'hall', 'toilet', 'cover_img', 'desc', 'housetype_id', 'money', 'img_content', 'cover_share_img', 'housetype_type'=>'housetype']);
- $find = MaterialCase::find($param['id']);
- if (!empty($param['community_name'])) {
- //新增小区
- if (strlen($param['community_name']) > 18) {
- return json(['code' => self::error_msg, 'msg' => '小区名称过长']);
- }
- $condition = [
- ['name', '=', $param['community_name']],
- ['type', '=', 0],
- ['root_id', '=', $this->request->token['root_org']]
- ];
- $CommunityId = Community::where($condition)->value('id');
- if(empty($CommunityId)) {
- $pinyin = hanzi2pinyin($param['community_name']);
- $community_id = Community::create([
- 'root_id' => $this->request->token['root_org'],
- 'pinyin' => $pinyin,
- 'name' => $param['community_name'],
- 'type' => 0,
- 'initials' => strtoupper(substr($pinyin,0,1)),
- 'employee_id'=> request()->token['employee_id']
- ]);
- $param['community_id'] = $community_id->id;
- }else{
- $param['community_id'] = $CommunityId;
- }
- }
- if (!empty($param['img_content'])) {
- $img_content_data = [];
- foreach ($param['img_content'] as $k => $v) {
- $img_content_data[$k] = [
- 'serverId' => $v['serverId'] ?? '',
- 'text' => $v['text'],
- 'img' => $v['img'] ?? ''
- ];
- }
- $param['img_content'] = '';
- $param['img_download_status'] = 1;
- $param['img_content_data'] = json_encode($img_content_data);
- }
- $param['publish'] = 0;
- $result = $find->save($param);
- Console::call('download', ['material_case']);
- if ($result) {
- return json(['code' => self::success, 'msg' => '保存成功']);
- } else {
- return json(['code' => self::error_msg, 'msg' => '保存失败']);
- }
- }
- }
|