250 lines
12 KiB
PHP
250 lines
12 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Notes:私域通
|
|
* Created on: 2021/1/13 14:44
|
|
* Created by: dengbw
|
|
*/
|
|
class Syt_entity
|
|
{
|
|
private $ci;
|
|
private $app_id;
|
|
private $app_config;
|
|
private $log_file;//日志文件
|
|
|
|
public function __construct($params = array())
|
|
{
|
|
$this->ci = &get_instance();
|
|
$this->init($params);
|
|
$class_name = lcfirst(get_class($this));//调用类的名称,子类或者当前类名称
|
|
$this->log_file = "libraris_{$class_name}.log";
|
|
$this->load->model('app/licheb/syt_activity_model', 'mdSytActivity');
|
|
$this->load->model('app/licheb/syt_activity_kpidata_model', 'mdSytActivityKpiData');
|
|
$this->load->model('app/licheb/syt_biz_score_model', 'mdSytBizScore');
|
|
}
|
|
|
|
public function init($params)
|
|
{
|
|
if ($params['app_id']) {
|
|
$this->app_id = $params['app_id'];
|
|
$this->load->model('app/App_model', 'mdApp');
|
|
$this->app_config = $this->ci->mdApp->appConfig()[$this->app_id];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Notes:报名记录
|
|
* Created on: 2022/1/24 10:57
|
|
* Created by: dengbw
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function kpi_enroll_log($params)
|
|
{
|
|
if ($params['cf_platform'] == 'wxapp') {
|
|
if (!$params['uid']) {
|
|
return array('code' => 400, 'msg' => '无狸车用户uid');
|
|
}
|
|
$this->load->model('app/liche/app_liche_users_model', 'mdLicheUsers');
|
|
$this->load->model('app/licheb/app_licheb_users_model', 'mdLichebUsers');
|
|
$this->load->model('app/app_weixin_users_model', 'mdWeixinUsers');
|
|
$re_lu = $this->ci->mdLicheUsers->get(array("id" => $params['uid']));
|
|
if (!$re_lu['unionid']) {
|
|
return array('code' => 400, 'msg' => '无狸车用户unionid');
|
|
}
|
|
$re_lw = $this->ci->mdWeixinUsers->get(array("unionid" => $re_lu['unionid']));
|
|
if (!$re_lw['unionid']) {
|
|
return array('code' => 400, 'msg' => '无狸车公众号unionid');
|
|
}
|
|
$jsondata = $re_lw['jsondata'] ? json_decode($re_lw['jsondata'], true) : '';
|
|
if (!$jsondata['syt']) {
|
|
return array('code' => 400, 'msg' => '无狸车公众号syt');
|
|
}
|
|
$syt = $jsondata['syt'];
|
|
$syt['nickname'] = $params['nickname'];
|
|
$syt['mobile'] = $params['mobile'];
|
|
$syt['uid'] = $re_lw['id'];
|
|
$syt['cf_id'] = 35;
|
|
$syt['if_driver'] = 1;
|
|
$params = $syt;
|
|
}
|
|
$this->load->model('receiver/receiver_customers_model', 'mdCustomers');
|
|
$this->load->model("biz/biz_model", 'mdBiz');
|
|
$re_a = $this->ci->mdSytActivity->get(array("id" => $params['a_id'], "status" => 1));
|
|
if (!$re_a) {
|
|
return array('code' => 400, 'msg' => '活动不存在');
|
|
}
|
|
$nickname = $params['nickname'];
|
|
$mobile = $params['mobile'];
|
|
$cf_id = intval($params['cf_id']);
|
|
$uid = intval($params['uid']);
|
|
$biz_id = intval($params['biz_id']);
|
|
$if_driver = intval($params['if_driver']);
|
|
if ($biz_id) {
|
|
$re_enroll = $this->ci->mdCustomers->get(array('cf_id' => $cf_id, 't_id' => $params['a_id'], 'mobile' => $mobile));
|
|
if ($re_enroll) {
|
|
return array('code' => 400, 'msg' => '你已经报名过啦,我们会在24时内与您取得联系,请耐心等待!');
|
|
}
|
|
}
|
|
$brand_id = 0;
|
|
if ($re_a['brand_id']) {
|
|
$brand_ids = implode(',', $re_a['brand_id']);
|
|
$brand_id = count($brand_ids) > 0 ? $brand_ids[0] : $re_a['brand_id'];
|
|
}
|
|
$c_id = 0;
|
|
$type = 'customers';
|
|
if ($biz_id) {//加入门店客户
|
|
$re_biz = $this->ci->mdBiz->get(['id' => $biz_id, 'status' => 1]);
|
|
if ($re_biz) {
|
|
$add_data = [
|
|
'name' => $nickname,
|
|
'mobile' => $mobile,
|
|
'biz_id' => $biz_id,
|
|
'brand_id' => $brand_id,
|
|
'city_id' => $re_biz['city_id'],
|
|
'county_id' => $re_biz['county_id'],
|
|
'cf_title' => '私域活动',
|
|
'cf_id' => $cf_id,
|
|
't_id' => $params['a_id'],
|
|
'p_time' => date('Y-m-d H:i:s'),
|
|
'c_time' => time()
|
|
];
|
|
$c_id = $this->ci->mdCustomers->add($add_data);
|
|
if (!$c_id) {
|
|
return array('code' => 400, 'msg' => '您报名该活动失败了');
|
|
}
|
|
}
|
|
}
|
|
if (!$c_id) {//未加到门店,加到线索池里
|
|
$this->load->model('receiver/receiver_clues_model', 'clues_model');
|
|
$this->load->library('receiver/clues_entity');
|
|
$row = $this->ci->clues_model->get(['mobile' => $mobile]);
|
|
if ($row) {
|
|
$update = ['en_time' => date('Y-m-d H:i:s')];
|
|
$this->ci->clues_model->update($update, ['id' => $row['id']]);
|
|
//增加日志
|
|
$cf_title = $this->ci->clues_entity->cf_title($cf_id);
|
|
$log_msg = '用户报名';
|
|
$cf_title && $log_msg = "用户通过【{$cf_title}】报名";
|
|
$this->ci->clues_entity->add_log($row['id'], $uid, $nickname, $log_msg);
|
|
return array('code' => 200, 'msg' => '报名成功了,我们会在24时内与您取得联系!');
|
|
}
|
|
$jsondata['info'] = ['biz_id' => $biz_id ? $biz_id : ''];
|
|
$params['a_id'] && $jsondata['info']['t_id'] = $params['a_id'];
|
|
$add_data = [
|
|
'name' => $nickname,
|
|
'mobile' => $mobile,
|
|
'brand_id' => $brand_id,
|
|
'cf_uid' => $uid,
|
|
'cf_id' => $cf_id,
|
|
'app_id' => $this->app_id,
|
|
'if_driver' => $if_driver,
|
|
'cf_platform' => $params['cf_platform'],
|
|
'jsondata' => json_encode($jsondata, JSON_UNESCAPED_UNICODE),
|
|
'en_time' => date('Y-m-d H:i:s'),
|
|
'c_time' => time()
|
|
];
|
|
$c_id = $this->ci->clues_model->add($add_data);
|
|
if (!$c_id) {
|
|
return array('code' => 400, 'msg' => '您报名该活动失败了');
|
|
}
|
|
$type = 'clues';
|
|
$cf_title = $this->ci->clues_entity->cf_title($cf_id);
|
|
$log_msg = '用户报名';
|
|
$cf_title && $log_msg = "用户通过【{$cf_title}】报名";
|
|
$this->ci->clues_entity->add_log($c_id, $uid, $nickname, $log_msg);
|
|
}
|
|
//增加报名分数
|
|
$this->kpi_log(array('a_id' => $params['a_id'], 'cf_uid' => $params['cf_uid'], 'biz_id' => $params['biz_id']
|
|
, 'kpi' => 'enroll', 'c_id' => $c_id, 'type' => $type, 'uid' => $uid));
|
|
return array('code' => 200, 'msg' => '报名成功,我们会在24时内与您取得联系!');
|
|
}
|
|
|
|
/**
|
|
* Notes:添加kpi记录
|
|
* Created on: 2021/1/13 11:20
|
|
* Created by: dengbw
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function kpi_log($params)
|
|
{
|
|
if ($params['a_id'] && $params['uid'] && ($params['cf_uid'] || $params['biz_id'])) {
|
|
$re_a = $this->ci->mdSytActivity->get(array("id" => $params['a_id'], "status" => 1));
|
|
$now_time = time();
|
|
if (!$re_a || $now_time < strtotime($re_a['s_time']) || $now_time > strtotime($re_a['e_time'])) {
|
|
return array('code' => 0, 'msg' => '无活动或未在活动时间内');
|
|
}
|
|
if ($params['kpi'] == 'share' && $params['cf_share']) {//检测分享码是否已使用
|
|
$where_share = array("a_id" => $params['a_id'], 'kpi' => $params['kpi'], "cf_share" => $params['cf_share']);
|
|
if ($params['cf_uid']) {
|
|
$where_share['cf_uid'] = $params['cf_uid'];
|
|
} else {
|
|
$where_share['biz_id'] = $params['biz_id'];
|
|
}
|
|
$re_share = $this->ci->mdSytActivityKpiData->get($where_share);
|
|
if ($re_share) {
|
|
return array('code' => 0, 'msg' => '分享码已被使用');
|
|
}
|
|
}
|
|
$re_kpi = $this->ci->mdSytActivityKpiData->get(array("a_id" => $params['a_id'], 'uid' => $params['uid'], 'kpi' => $params['kpi']));
|
|
if ($re_kpi) {//已添加过活动kpi(访活动只记录一次用户kpi记录)
|
|
return array('code' => 0, 'msg' => '已添加过' . $params['kpi'] . '记录');
|
|
}
|
|
$where_kpi = array("a_id" => $params['a_id'], "biz_id" => $params['biz_id'], 'kpi' => $params['kpi']);
|
|
//增加记录
|
|
$addData = array_merge($where_kpi, array('uid' => $params['uid'], 'cf_uid' => $params['cf_uid'], 'c_time' => time()));
|
|
$jsondata = array();
|
|
if ($params['kpi'] == 'enroll' && $params['c_id']) {//加报名id记录
|
|
$jsondata['enroll_type'] = $params['type'];
|
|
$jsondata['enroll_c_id'] = $params['c_id'];
|
|
}
|
|
if ($params['kpi'] == 'share' && $params['cf_share']) {//加分享码
|
|
$addData['cf_share'] = $params['cf_share'];
|
|
}
|
|
$jsondata && $addData['jsondata'] = json_encode($jsondata, JSON_UNESCAPED_UNICODE);
|
|
$id = $this->ci->mdSytActivityKpiData->add($addData);
|
|
if (!$id) {
|
|
return array('code' => 0, 'msg' => '添加记录失败');
|
|
}
|
|
if (!$params['biz_id']) {
|
|
return array('code' => 0, 'msg' => '无门店不加分数');
|
|
}
|
|
$jsonkpi = $re_a['jsonkpi'] ? json_decode($re_a['jsonkpi'], true) : array();
|
|
$kpi_count = $this->ci->mdSytActivityKpiData->count(array_merge($where_kpi, array('status' => 0)));
|
|
$kpi_num = intval($jsonkpi[$params['kpi']]['num']);
|
|
$kpi_score = intval($jsonkpi[$params['kpi']]['score']);
|
|
if ($kpi_num && $kpi_score && $kpi_count >= $kpi_num) {//大于等于次数加分
|
|
$re_biz_score = $this->ci->mdSytBizScore->get(array('a_id' => $params['a_id'], 'biz_id' => $params['biz_id']));
|
|
if (!$re_biz_score) {//未添加门店数据
|
|
$re_biz_score['id'] = $this->ci->mdSytBizScore->add(['a_id' => $params['a_id'], 'biz_id' => $params['biz_id'], 'c_time' => time()]);
|
|
if (!$re_biz_score['id']) {
|
|
return array('code' => 0, 'msg' => '添加门店分数记录');
|
|
}
|
|
$re_biz_score['score'] = 0;
|
|
}
|
|
$score_ms = $kpi_score + intval($re_biz_score['score']);
|
|
$this->ci->mdSytBizScore->update(array('score' => $score_ms), array('id' => $re_biz_score['id']));
|
|
$res_kpi = $this->ci->mdSytActivityKpiData->select(array_merge($where_kpi, array('status' => 0)), 'id asc', 0, $kpi_num, 'id');
|
|
if ($res_kpi) {
|
|
$kpi_ids = implode(',', array_column($res_kpi, "id"));
|
|
$this->ci->mdSytActivityKpiData->update(array('status' => 1), array("id in(" . $kpi_ids . ")" => null));//更新为已加分
|
|
}
|
|
return array('code' => 1, 'msg' => '添加记录成功');
|
|
}
|
|
} else {
|
|
return array('code' => 0, 'msg' => '参数错误');
|
|
}
|
|
}
|
|
|
|
public function __get($name)
|
|
{
|
|
if ('_model' === substr($name, -6)) {
|
|
return $this->ci->$name;
|
|
} elseif ('load' == $name) {
|
|
return $this->ci->load;
|
|
}
|
|
return null;
|
|
}
|
|
}
|