123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <?php
- namespace app\api\controller;
- use app\model\FishCampaign;
- use app\model\FishCampaignList;
- use app\model\Company;
- use app\model\CustomerSource;
- use app\model\CustomerVisitLog;
- use app\model\FishLive;
- use app\model\OutCallLog;
- use think\facade\Db;
- use clue\Tencent;
- use app\model\Org;
- use app\model\FishData;
- use clue\FishWh as clueFish;
- class FishWh
- {
- /**
- * 飞鱼外呼回调
- */
- public function fish_phone_call_back()
- {
- $data = input();
- if (isset($data['event']) && $data['event'] == 'verify_webhook') {
- //返回数据
- $this->ajax_return($data['challenge']);
- }
- if (isset($data['data']) && $data['data']) {
- $tmp_data = json_decode($data['data'], true);
- if (isset($tmp_data['event']) && $tmp_data['event'] == 'CONTACT_LOG') {
- $call_data = json_decode($tmp_data['content'], true);
- $log = OutCallLog::where(['session_id' => $call_data['contactId']])->find();
- if (empty($log)) return json(['code' => 0, 'msg' => '通话记录不存在']);
- $startTime = is_int($call_data['startTime']) ? date('Y-m-d H:i:s', $call_data['startTime'] / 1000) : $call_data['startTime'];
- $endTime = is_int($call_data['endTime']) ? date('Y-m-d H:i:s', $call_data['endTime'] / 1000) : $call_data['endTime'];
- $log->save([
- 'fwd_start_time' => $startTime,
- 'call_end_time' => $endTime,
- 'billsec' => $call_data['duration'],
- 'status' => $call_data['duration'] > 0 ? 1 : -1,
- 'call_status' => 1000,
- 'url' => $call_data['audioUrl'],
- 'call_msg' => '飞鱼线索-' . $call_data['endStateShowMsg']
- ]);
- $visitLog = CustomerVisitLog::where(['data_type' => 'out_call', 'data_id' => $log->id])->find();
- if (!empty($visitLog)) {
- if ($call_data['duration'] > 0) {
- $visitLog->remark = '回访跟踪了飞鱼线索客户,通话时长' . $call_data['duration'] . 's';
- } else {
- $visitLog->remark = '回访跟踪了飞鱼线索客户,电话未接通';
- }
- $visitLog->save();
- }
- }
- }
- //返回数据
- // Company::where("root_id = 1")->update(['channel_setting_wh'=>json_encode($data)]);
- $this->ajax_return($data['nonce']);
- }
- /**
- * 飞鱼返回
- */
- public function ajax_return($data)
- {
- $return = array(
- 'BaseResp' => ['StatusCode' => 200, 'StatusMessage' => 'ok'],
- 'challenge' => intval($data),
- );
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($return, JSON_UNESCAPED_UNICODE));
- }
- /**
- * 飞鱼授权回调
- */
- public function fish_call_back()
- {
- $code = input('auth_code');
- $state = input('state');
- $appid = config('app.fish_clue_appid_wh');
- $secret = config('app.fish_clue_secret_wh');
- $data = (new clueFish($appid, $secret))->getAccessToken($code);
- if ($data['code'] == 0) {
- $channel_setting_wh = Company::where(['root_id' => $state])->value('channel_setting_wh');
- $tmp_data = [
- 'fish_account' => '',
- 'fish_adv' => '',
- 'fish_access_token' => '',
- 'fish_refresh_token' => '',
- 'fish_refresh_time' => 0,
- 'fish_update_time' => '',
- 'fish_open' => 0,
- 'tx_account' => '',
- 'tx_adv' => '',
- 'tx_open' => 0,
- 'tx_access_token' => '',
- 'tx_refresh_token' => '',
- 'tx_update_time' => ''
- ];
- if ($channel_setting_wh) {
- $tmp_data = json_decode($channel_setting_wh, true);
- }
- $tmp_data['fish_access_token'] = $data['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $data['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $state])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- $fish = CustomerSource::where(['root_id' => $state, 'source' => '飞鱼线索'])->find();
- if (!$fish) {
- CustomerSource::create(['root_id' => $state, 'source' => '飞鱼线索']);
- }
- return redirect('/dashboard/index/index');
- }
- return redirect('/dashboard/index/index');
- }
- /**
- * 每天获取一次昨日的数据(数据稳定)保存入数据库-同时更新广告组下的广告计划--需要定时任务
- */
- public function update_fish_campaign_yesterday()
- {
- //查找存在配置的公司
- $list = Company::where("channel_setting_wh <> 'null'")->field('root_id,channel_setting_wh')->select()->toArray();
- if (isset($list) && $list) {
- $fish = new clueFish(config('app.fish_clue_appid_wh'), config('app.fish_clue_secret_wh'));
- foreach ($list as $val) {
- if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
- $root_id = $val['root_id'];
- $tmp_data = json_decode($val['channel_setting_wh'], true);
- $start_date = date('Y-m-d', strtotime('-1 day'));
- //更新飞鱼
- $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach ($advertiser_id as $va) {
- $data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => 1]);
- if ($data['code'] == 0) {
- //如果广告组数据存在多页
- $page = $data['data']['page_info']['total_page'];
- $this->update_fish_campaign_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- if ($page > 1) {
- for ($i = 1; $i < $page; $i++) {
- $page_data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
- if ($page_data['code'] == 0) {
- $this->update_fish_campaign_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- }
- }
- }
- }
- }
- }
- }
- }
- return json(['code' => 0, 'msg' => '更新成功']);
- }
- /**
- * 获取今日数据
- */
- public function update_fish_campaign_today()
- {
- //查找存在配置的公司
- $list = Company::where("channel_setting_wh <> 'null'")->field('root_id,channel_setting_wh')->select()->toArray();
- if (isset($list) && $list) {
- $appid = config('app.fish_clue_appid_wh');
- $secret = config('app.fish_clue_secret_wh');
- $fish = new clueFish($appid, $secret);
- foreach ($list as $val) {
- if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
- $root_id = $val['root_id'];
- $tmp_data = json_decode($val['channel_setting_wh'], true);
- $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
- $start_date = date('Y-m-d');
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach ($advertiser_id as $va) {
- $data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => 1]);
- if ($data['code'] == 0) {
- //如果广告组数据存在多页
- $page = $data['data']['page_info']['total_page'];
- $this->update_fish_campaign_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- if ($page > 1) {
- for ($i = 1; $i < $page; $i++) {
- $page_data = $fish->getCampaignList($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
- if ($page_data['code'] == 0) {
- $this->update_fish_campaign_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- }
- }
- }
- }
- }
- }
- }
- }
- return json(['code' => 0, 'msg' => '更新成功']);
- }
- /**
- * 更新数据
- */
- public function update_fish_campaign_data($list, $fish, $tmp_data, $root_id, $data_time, $advertiser_id)
- {
- $data = [];
- foreach ($list as $val) {
- //先删除昨日数据后插入数据
- FishCampaign::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id, 'campaign_id' => $val['campaign_id']])->delete();
- FishCampaignList::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id, 'campaign_id' => $val['campaign_id']])->delete();
- $val['advertiser_id'] = $advertiser_id;
- $val['data_time'] = $data_time;
- $val['root_id'] = $root_id;
- unset($val['id']);
- $data[] = $val;
- //循环广告组获取广告组下的计划花费
- $adv_data = $fish->getAdList($tmp_data['fish_access_token'], ['advertiser_id' => $advertiser_id, 'campaign_ids' => $val['campaign_id'], 'start_date' => $data_time, 'end_date' => $data_time, 'page' => 1]);
- if ($adv_data['code'] == 0) {
- $page = $adv_data['data']['page_info']['total_page'];
- $this->update_fish_adv_data($adv_data['data']['list'], $root_id, $data_time, $advertiser_id);
- //如果广告计划存在多页
- if ($page > 1) {
- for ($i = 1; $i < $page; $i++) {
- $tmp_adv_data = $fish->getAdList($tmp_data['fish_access_token'], ['advertiser_id' => $advertiser_id, 'campaign_ids' => $val['campaign_id'], 'start_date' => $data_time, 'end_date' => $data_time, 'page' => $i + 1]);
- if ($tmp_adv_data['code'] == 0) {
- $this->update_fish_adv_data($tmp_adv_data['data']['list'], $root_id, $data_time, $advertiser_id);
- }
- }
- }
- }
- }
- FishCampaign::insertAll($data);
- }
- /**
- * 更新广告组下的计划
- */
- public function update_fish_adv_data($data, $root_id, $data_time, $advertiser_id)
- {
- $tmp_adv_list = [];
- foreach ($data as $val) {
- unset($val['id']);
- unset($val['avg_show_cost']);
- $val['root_id'] = $root_id;
- $val['advertiser_id'] = $advertiser_id;
- $val['data_time'] = $data_time;
- $tmp_adv_list[] = $val;
- }
- FishCampaignList::insertAll($tmp_adv_list);
- }
- /**
- * 刷新token
- */
- public function refreshToken($fish, $tmp_data, $root_id)
- {
- $fish_refresh_time = $tmp_data['fish_refresh_time']; //上次token更新时间
- if ($fish_refresh_time + 80000 < time()) {
- $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
- if ($token['code'] == 0) {
- $tmp_data['fish_access_token'] = $token['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- } else {
- $tmp_data['fish_access_token'] = '';
- $tmp_data['fish_refresh_token'] = '';
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- }
- }
- return $tmp_data;
- }
- /**
- * 获取直播动态数据
- * 3分钟更新一次API 994
- * 分页处理定时任务,limit 50
- */
- public function update_fish_live_today()
- {
- $test_ads = '1765746298844174,1765954841526279';
- $root_id = 994;
- //查找存在配置的公司
- $list = Company::where("channel_setting_wh <> 'null'")
- ->field('root_id,channel_setting_wh')
- ->where('root_id',$root_id)
- ->limit(50)
- ->select()->toArray();
- if (isset($list) && $list) {
- $appid = config('app.fish_clue_appid_wh');
- $secret = config('app.fish_clue_secret_wh');
- $fish = new clueFish($appid, $secret);
- foreach ($list as $val) {
- if(isset($val['channel_setting_wh']) && $val['channel_setting_wh']){
- $root_id = $val['root_id'];
- $tmp_data = json_decode($val['channel_setting_wh'], true);
- $tmp_data = $this->refreshToken($fish, $tmp_data, $root_id);
- $start_date = date('Y-m-d 00:00:00');
- $end_date = date('Y-m-d 23:59:59');
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach ($advertiser_id as $va) {
- $data = $fish->getLiveAttribute(
- $tmp_data['fish_access_token'],
- [
- 'advertiser_id' => $va,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'page' => 1
- ]);
- // dump($data);die;
- if ($data['code'] == 0) {
- //如果广告组数据存在多页
- $page = $data['data']['page_info']['total_page'];
- $this->update_fish_live_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- if ($page > 1) {
- for ($i = 1; $i < $page; $i++) {
- $page_data = $fish->getLiveAttribute($tmp_data['fish_access_token'], ['advertiser_id' => $va, 'start_date' => $start_date, 'end_date' => $start_date, 'page' => $i + 1]);
- if ($page_data['code'] == 0) {
- $this->update_fish_live_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- }
- }
- }
- }
- }
- $fields = [
- 'live_watch_count',
- 'live_watch_ucount',
- 'click_product_ucount',
- 'live_form_submit_count',
- 'live_card_icon_component_show_count',
- 'live_avg_watch_duration',
- 'live_comment_count',
- 'live_follow_count',
- 'live_share_count',
- 'live_count',
- ];
- foreach ($advertiser_id as $va) {
- $data = $fish->getAnalysisLiveList(
- $tmp_data['fish_access_token'],
- [
- 'advertiser_id' => $va,
- 'start_date' => $start_date,
- 'end_date' => $end_date,
- 'page' => 1,
- 'fields' => $fields
- ]);
- // dump($data);die;
- if ($data['code'] == 0) {
- //如果广告组数据存在多页
- $page = $data['data']['page_info']['total_page'];
- $this->update_fish_live_data($data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- if ($page > 1) {
- for ($i = 1; $i < $page; $i++) {
- $page_data = $fish->getAnalysisLiveList(
- $tmp_data['fish_access_token'],
- [
- 'advertiser_id' => $va,
- 'start_date' => $start_date,
- 'end_date' => $start_date,
- 'page' => $i + 1,
- 'fields' => $fields
- ]);
- if ($page_data['code'] == 0) {
- $this->update_fish_live_data($page_data['data']['list'], $fish, $tmp_data, $root_id, $start_date, $va);
- }
- }
- }
- }
- }
- }
- }
- }
- return json(['code' => 0, 'msg' => '更新成功']);
- }
- /**
- * 更新直播数据
- */
- protected function update_fish_live_data($list, $fish, $tmp_data, $root_id, $data_time, $advertiser_id)
- {
- $data_time = date('Y-m-d',strtotime($data_time));
- Db::startTrans();
- try {
- foreach ($list as $val) {
- //先删除昨日数据后插入数据
- $info = FishLive::where(['root_id' => $root_id, 'data_time' => $data_time, 'advertiser_id' => $advertiser_id,'room_id'=>$val['room_id']])->find();
- $param = [
- 'root_id' => $root_id,
- 'room_id' => $val['room_id'],
- 'data_time' => $data_time,
- 'advertiser_id' => $advertiser_id,
- 'room_title' => $val['room_title']?$val['room_title']:'',
- 'live_watch_count' => $val['fields']['live_watch_count']?$val['fields']['live_watch_count']:0,
- 'live_watch_ucount' => $val['fields']['live_watch_ucount']?$val['fields']['live_watch_ucount']:0,
- 'live_card_icon_component_show_count' => $val['fields']['live_card_icon_component_show_count']?$val['fields']['live_card_icon_component_show_count']:0,
- 'click_product_ucount' => $val['fields']['click_product_ucount']?$val['fields']['click_product_ucount']:0,
- 'live_form_submit_count' => $val['fields']['live_form_submit_count']?$val['fields']['live_form_submit_count']:0,
- 'room_status' => $val['room_status'],
- 'type' => 1,
- 'room_create_time' => !empty($val['room_create_time'])?date('Y-m-d H:i:s',strtotime($val['room_create_time'])):'',
- 'room_finish_time' => !empty($val['room_finish_time'])?date('Y-m-d H:i:s',strtotime($val['room_finish_time'])):'',
- 'anchor_id' => $val['anchor_id']?$val['anchor_id']:'',
- 'anchor_nick' => $val['anchor_nick']?$val['anchor_nick']:'',
- 'data_create' => date('Y-m-d',strtotime($val['room_create_time'])),
- 'live_avg_watch_duration' => $val['fields']['live_avg_watch_duration']?$val['fields']['live_avg_watch_duration']:0,
- 'live_comment_count' => $val['fields']['live_comment_count']?$val['fields']['live_comment_count']:0,
- 'live_follow_count' => $val['fields']['live_follow_count']?$val['fields']['live_follow_count']:0,
- 'live_share_count' => $val['fields']['live_share_count']?$val['fields']['live_share_count']:0,
- 'live_count' => $val['fields']['live_count']?$val['fields']['live_count']:0,
- ];
- $where = [
- 'root_id' => $root_id,
- 'room_id' => $val['room_id'],
- 'data_time' => $data_time,
- 'advertiser_id' => $advertiser_id,
- ];
- if ($info){
- FishLive::where($where)->update($param);
- }else{
- FishLive::where($where)->insert($param);
- }
- }
- Db::commit();
- } catch (\Exception $e) {
- file_put_contents(dirname(app()->getRuntimePath()) . DIRECTORY_SEPARATOR.'log'.'/update_fish_live_data.txt', date('Y-m-d H:i:s') ."\n"."更新直播数据:".$e->getMessage()."\r\n", FILE_APPEND);
- Db::rollback();
- }
- }
- //飞鱼推送线索
- public function fish_clue_back()
- {
- //查找存在配置的公司
- $data = input();
- $fid = FishData::where(['clue_id' => $data['id'], 'type' => 4])->find();
- if (!isset($fid) && empty($fid)){
- $save['gender'] = $data['gender'] == '男' ? 1 : ($data['gender'] == '女' ? 2 : 0);
- $save['promotion_id'] = $data['promotion_id'];
- if(isset($data['req_id']))$save['req_id'] = $data['req_id'];
- if(isset($data['external_url']))$save['external_url'] = $data['external_url'];
- $save['remark_dict'] = json_encode($data['remark_dict']);
- $save['advertiser_id'] = $data['adv_id'];//广告主id
- $save['clue_id'] = $data['id'];
- if(isset($data['adv_name']))$save['advertiser_name'] = $data['adv_name'];
- $save['convert_status'] = $data['clue_convert_status'];
- $save['create_time'] = date('Y-m-d',$data['create_time']);
- $save['create_time_detail'] = date('Y-m-d H:i:s',$data['create_time']);
- if(isset($data['ad_name']))$save['ad_name'] = $data['ad_name'];
- $save['clue_source'] = $data['clue_source'];
- if(isset($data['site_name']))$save['site_name'] = $data['site_name'];
- $save['app_name'] = $data['app_name'];
- if(isset($data['ad_id']))$save['ad_id'] = $data['ad_id'];
- $save['module_id'] = $data['module_id'];
- $save['telephone'] = $data['telphone'];
- $save['name'] = $data['name'];
- $save['site_id'] = $data['site_id'];
- $save['clue_type'] = $data['clue_type'];
- $save['location'] = $data['location'];
- $save['module_name'] = $data['module_name'];
- if(isset($data['cid']))$save['creative_id'] = $data['cid'];
- //检查广告主是否存在-飞鱼上需要设置企业ID和默认部门名称
- $root_id = $data['root_id'];
- $org_id = $data['org_name'];//2361
- $save['type'] = 4;
- // $org_id = Org::where(['pid' => $root_id, 'name' => $org_name])->value('id');
- $company_info = Company::where(['root_id' => $root_id])->value('id');
- if (!empty($company_info) && isset($org_id)) {
- $save['root_id'] = $root_id;
- $save['org_id'] = $org_id;
- $save['employee_id'] = 0;
- FishData::insert($save);
- }
- }
- //返回信息
- $return = array(
- 'code' => 0,
- 'message' => 'success',
- );
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($return, JSON_UNESCAPED_UNICODE));
- }
- /**
- * todo 同步直播飞鱼数据
- */
- public function synchronize_fish_live_data()
- {
- $request = request();
- $root_id = 994;//$request->empcrm->root_id;
- $channel_setting = Company::where(['root_id' => $root_id])->value('channel_setting_wh');
- if($channel_setting){
- $tmp_data = json_decode($channel_setting, true);
- if($tmp_data['fish_adv']) {
- $appid = config('app.fish_clue_appid_wh');
- $secret = config('app.fish_clue_secret_wh');
- $fish_update_time = $tmp_data['fish_update_time'] ?: date('Y-m-d H:i:s', time() - 86400 * 180);
- $end_time = date('Y-m-d H:i:s');
- $page_siae = 100;
- $fish = new \clue\FishWh($appid, $secret);
- $fish_refresh_time = $tmp_data['fish_refresh_time'];//上次token更新时间
- if($fish_refresh_time + 80000 < time()){
- $token = $fish->getRefreshToken($tmp_data['fish_refresh_token']);
- if ($token['code'] == 0){
- $tmp_data['fish_access_token'] = $token['data']['access_token'];
- $tmp_data['fish_refresh_token'] = $token['data']['refresh_token'];
- $tmp_data['fish_refresh_time'] = time();
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- }else {
- $tmp_data['fish_access_token'] = '';
- $tmp_data['fish_refresh_token'] = '';
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- }
- }
- $advertiser_id = explode(',', $tmp_data['fish_adv']);
- foreach($advertiser_id as $adv){
- $data = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, 1, $page_siae);
- // dump($data);die;
- if (isset($data) && $data['code'] == 0) {
- $total_number = $data['data']['page_info']['total_number'];
- $page = intval(ceil($total_number / $page_siae));
- $list = $data['data']['list'];
- Db::startTrans();
- try {
- $this->processing_fish_data($list, $root_id);
- for ($i = 1; $i < $page; $i++) {
- $tmp = $fish->getClueListData($tmp_data['fish_access_token'], $adv, $fish_update_time, $end_time, $i + 1, $page_siae);
- $this->processing_fish_data($tmp['data']['list'], $root_id);
- }
- $tmp_data['fish_update_time'] = $end_time;
- Company::where(['root_id' => $root_id])->update(['channel_setting_wh' => json_encode($tmp_data)]);
- Db::commit();
- } catch (\Exception $e) {
- trace($e->getMessage(), 'error');
- Db::rollback();
- }
- }
- }
- }
- }
- }
- //保存飞鱼线索
- public function processing_fish_data($list, $root_id)
- {
- $customer = [];
- foreach ($list as $val) {
- //查找所属员工-只有员工名称对应上才允许保持到本地库
- $val['extra_info'] = implode(',', $val['extra_info']);
- $val['remark_dict'] = json_encode($val['remark_dict']);
- $val['store'] = json_encode($val['store']);
- $val['tags'] = implode(';', $val['tags']);
- $val['root_id'] = $root_id;
- $info = Employee::where(['name' => $val['clue_owner_name'], 'root_id' => $root_id])->field('id,org_id')->find();
- //检查线索是否存在
- $fid = FishData::where(['clue_id' => $val['clue_id'], 'type' => 1])->find();
- if (!isset($fid) && isset($info) && $info) {
- $val['employee_id'] = $info['id'];
- $val['org_id'] = $info['org_id'];
- $customer[] = $val;
- }
- //检查线索是否被分配
- if (isset($fid) && empty($fid['clue_owner_name'])) {
- $val['employee_id'] = $info['id'];
- $val['org_id'] = $info['org_id'];
- FishData::where(['clue_id' => $val['clue_id'], 'type' => 1])->update($val);
- }
- }
- FishData::insertAll($customer);
- }
- }
|