Files
2025-12-09 09:41:17 +08:00

342 lines
14 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Date: 2021/06/29
* Time: 13:47
*/
defined('BASEPATH') or exit('No direct script access allowed');
class Receiver_clues_model extends HD_Model
{
private $table_name = 'lc_receiver_clues';
const APP_ID_ACTIVITY = 1; //来源活动
const CF_ID2_PRODUCT = 40; // cf_id2 来源产品
//意向购车周期
// const BUY_TIME_LIST = [1 => '3天内', 2 => '一周内', 3 => '15天内', 4 => '1个月内', 5 => '超过1个月'];
const BUY_TIME_LIST = [7 => '7天内', 15 => '15天内', 30 => '一个月内', 90 => '三个月内', 120 => '三个月以上'];
const LEVEL_LIST = [7 => 'H', 15 => 'A', 30 => 'B', 90 => 'C', 120 => 'D'];
const LEVEL_DEFAULT = 'H';//默认等级
//分佣机构id
const COMM_ORG_IDS = [1];
//省份对应归属id
const PROVINCE_BELONG = [
'460000' => 39, //海南省
'350000' => 10, //福建
'440000' => 99, //广东省
];
public function __construct()
{
parent::__construct($this->table_name, 'default');
}
/**
* Notes:线索状态
* Created on: 2021/9/15 10:25
* Created by: dengbw
* @param $status
* @return array
*/
public function statusAry($status = '')
{
$status_ary[0] = array('name' => '待处理', 'list' => array());
$status_ary[2] = array('name' => '跟进中', 'list' => array(4 => '未接通', 5 => '待派单', 6 => '待挖掘', '7' => '拒绝派单', '8' => '未完整触碰'));
$status_ary[1] = array('name' => '已派单', 'list' => array(1 => '待转交', 2 => '已转交'));
$status_ary[3] = array('name' => '无效线索', 'list' => array(6 => '空号', 7 => '已买车', 8 => '无意向', 9 => '非本人'));
// $status_ary[1] = array('name' => '已分配', 'list' => array());
// $status_ary[2] = array('name' => '跟进中', 'list' => array());
// $status_ary[3] = array('name' => '无效线索', 'list' => array(7 => '明确拒绝', 8 => '误报', 9 => '战败'));
if (strlen($status)) {
$return_status = $status_ary[$status];
} else {
$return_status = $status_ary;
}
return $return_status;
}
//关联订单
public function count_order($where)
{
return $this->select_order($where, '', '', '', '', 1);
}
public function select_order($where = array(), $order = '', $page = 0, $page_size = 20, $fileds = '', $count = 0)
{
!$fileds && $fileds = 'lc_receiver_clues.*';
$this->db->select($fileds);
$this->db->from('lc_receiver_clues');
$this->db->join('lc_receiver_orders_v2', 'lc_receiver_orders_v2.clue_id = lc_receiver_clues.id', 'left');
if ($where) {
$this->db->where($where);
}
if ($count) {
return $this->db->count_all_results();
}
if ($order) {
$this->db->order_by($order);
}
if ($page) {
$offset = ($page - 1) * $page_size;
$limit = $page_size;
} else {
$offset = null;
$limit = null;
}
$this->db->limit($limit, $offset);
return $this->db->get()->result_array();
}
public function selectClues($where = array(), $order = '', $page = 0, $page_size = 20, $count = 0, $fileds = '')
{
$this->db->from('lc_receiver_clues');
$this->db->join('lc_receiver_orders_v2', 'lc_receiver_orders_v2.clue_id = lc_receiver_clues.id and lc_receiver_orders_v2.status>=0', 'left');
if ($where) {
$this->db->where($where);
}
if ($count) {
$this->db->distinct()->select('lc_receiver_clues.*');
return $this->db->count_all_results();
} else {
if ($fileds) {
$this->db->select($fileds);
} else {
$this->db->select('lc_receiver_clues.*,lc_receiver_orders_v2.id as o_id');
}
$this->db->group_by('lc_receiver_clues.id');
}
if ($order) {
$this->db->order_by($order);
}
if ($page) {
$offset = ($page - 1) * $page_size;
$limit = $page_size;
} else {
$offset = null;
$limit = null;
}
$this->db->limit($limit, $offset);
return $this->db->get()->result_array();
}
/**
* @param $id
* @param $bizId
* @return MyResponse
*/
public function setCluesCouponStatus($id, $bizId)
{
$this->load->model('agent/auto_user_coupon_model');
$this->auto_user_coupon_model->set_db('agentdb');
try {
$clues_row = $this->get(['id' => $id]);
if (!$clues_row) {
throw new Exception("线索不存在");
}
$coupon = '';
$where = [
'productId' => $clues_row['out_id'],
'userId' => $clues_row['cf_uid']
];
$couponRows = $this->auto_user_coupon_model->select($where, 'id desc', 1, 1);
$couponRows && $coupon = $couponRows[0];
if (!$coupon) {
throw new Exception("优惠券不存在");
}
$update = [
'status' => Auto_user_coupon_model::STATUS_NOT_USED,
'bizId' => $bizId
];
$res = $this->auto_user_coupon_model->update($update, ['id' => $coupon['id']]);
if (!$res) {
throw new Exception("设置优惠券状态失败");
}
return new MyResponse(EXIT_SUCCESS, 'success');
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, '设置优惠券状态失败:' . $e->getMessage());
}
}
/**
* 根据线索id获取优惠券信息
* @param $id
* @return MyResponse
*/
public function getCluesCoupon($id)
{
$this->load->model('agent/auto_user_coupon_model');
$this->auto_user_coupon_model->set_db('agentdb');
try {
$clues_row = $this->get(['id' => $id]);
if (!$clues_row) {
throw new Exception("线索不存在");
}
$coupon = '';
$where = [
'productId' => $clues_row['out_id'],
'userId' => $clues_row['cf_uid']
];
$couponRows = $this->auto_user_coupon_model->select($where, 'id desc', 1, 1);
$couponRows && $coupon = $couponRows[0];
if (!$coupon) {
throw new Exception("优惠券不存在");
}
return new MyResponse(EXIT_SUCCESS, 'success', $coupon);
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, '设置优惠券状态失败:' . $e->getMessage());
}
}
/**
* 分佣
* @param int $cluesId 线索id
* @param int $bizId 门店id
* @param float $money 分佣金额
* @return MyResponse
*/
public function Commissions($cluesId, $bizId, $money)
{
$this->load->helper('string');
$this->load->model('receiver/receiver_clues_model');
$this->load->model('agent/pingan/pingan_users_model');
$this->load->model('agent/organization/organization_cmmssn_model', 'mdOrganizationCmmssn');
$this->load->model('agent/organization/organization_model');
$this->load->model('agent/pingan/pingan_users_cmmssn_model');
$this->load->model('agent/auto_product_model');
$this->auto_product_model->set_db('agentdb');
$this->pingan_users_model->set_db('agentdb');
$this->mdOrganizationCmmssn->set_db('agentdb');
$this->organization_model->set_db('agentdb');
$this->pingan_users_cmmssn_model->set_db('agentdb');
$log_path = 'commissions.log';
$log_dir = 'clues';
try {
debug_log("开始分佣:线索id:{$cluesId},门店id:{$bizId},分佣金额:{$money}", $log_path, $log_dir);
$clues = $this->receiver_clues_model->get(['id' => $cluesId, 'app_id' => Receiver_clues_model::APP_ID_ACTIVITY]);
if (!$clues) {
throw new Exception("线索不存在");
}
if (!$clues['out_id']) {
throw new Exception("报名来源不存在");
}
if (!$clues['pingan_user_id']) {
debug_log("结束分佣:线索不需要分佣", $log_path, $log_dir);
return new MyResponse(EXIT_SUCCESS, '保存成功');
}
$pinanUser = $this->pingan_users_model->get(['id' => $clues['pingan_user_id']]);
if (!$pinanUser['id']) {
throw new Exception("线索不存在绑定用户关系");
}
//修改获取分佣金额
$product = $this->auto_product_model->get(['id' => $clues['out_id']]);
$money = $product['cluesCommission'] ?: $money; //商家扣除金额
//获取分佣配置
$cmmssn = $this->mdOrganizationCmmssn->get(['teamId' => $pinanUser['orgTeamId'], 'status' => 0]);
if (!$cmmssn) {
throw new Exception("分佣配置不存在");
}
//团队设置分佣等级
$level = $this->organization_model->getTeamLevel($pinanUser['orgTeamId']);
$brokerage1 = $money * ($cmmssn['cluesLevel1'] / 100) ?: 0;
$brokerage2 = $money * ($cmmssn['cluesLevel2'] / 100) ?: 0;
$brokerage3 = $money * ($cmmssn['cluesLevel3'] / 100) ?: 0;
$brokerage4 = $money * ($cmmssn['cluesLevel4'] / 100) ?: 0;
debug_log('分佣金额:' . json_encode([$brokerage1, $brokerage2, $brokerage3, $brokerage4]), $log_path, $log_dir);
$cfId = $clues['id'];
$sourceId = $clues['out_id'];
$add_data = [];
if ($brokerage1 && $clues['area_id']) { //一级佣金
$add_data[] = [
'cfId' => $cfId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_CLUES_TEAM,
'money' => $brokerage1,
'pinganUserId' => $clues['area_id'],
'teamId' => 0,
'depId' => 0,
'areaId' => $clues['area_id'],
'createTime' => date('Y-m-d H:i:s'),
'bizId' => $bizId,
'sourceId' => $sourceId
];
}
if ($brokerage2 && $clues['dep_id']) { //二级佣金
$add_data[] = [
'cfId' => $cfId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_CLUES_TEAM,
'money' => $brokerage2,
'pinganUserId' => $clues['dep_id'],
'teamId' => 0,
'depId' => $clues['dep_id'],
'areaId' => $clues['area_id'] ?: 0,
'createTime' => date('Y-m-d H:i:s'),
'bizId' => $bizId,
'sourceId' => $sourceId
];
}
if ($level >= 4) { //4级
if ($brokerage3 && $clues['team_id']) {
$add_data[] = [
'cfId' => $cfId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_CLUES_TEAM,
'money' => $brokerage3,
'pinganUserId' => $clues['team_id'],
'teamId' => $clues['team_id'],
'depId' => $clues['dep_id'],
'areaId' => $clues['area_id'] ?: 0,
'createTime' => date('Y-m-d H:i:s'),
'bizId' => $bizId,
'sourceId' => $sourceId
];
}
if ($brokerage4 && $clues['pingan_user_id']) {
$add_data[] = [
'cfId' => $cfId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_CLUES_OWN,
'money' => $brokerage4,
'pinganUserId' => $clues['pingan_user_id'],
'teamId' => $clues['team_id'],
'depId' => $clues['dep_id'],
'areaId' => $clues['area_id'] ?: 0,
'createTime' => date('Y-m-d H:i:s'),
'bizId' => $bizId,
'sourceId' => $sourceId
];
}
} else { //三级
if ($brokerage3 && $clues['pingan_user_id']) {
$add_data[] = [
'cfId' => $cfId,
'cfType' => Pingan_users_cmmssn_model::CF_TYPE_CLUES_TEAM,
'money' => $brokerage3,
'pinganUserId' => $clues['team_id'],
'teamId' => 0,
'depId' => $clues['dep_id'],
'areaId' => $clues['area_id'] ?: 0,
'createTime' => date('Y-m-d H:i:s'),
'bizId' => $bizId,
'sourceId' => $sourceId
];
}
}
debug_log('分佣数据:' . json_encode($add_data), $log_path, $log_dir);
if (count($add_data)) {
$result = $this->pingan_users_cmmssn_model->add_batch($add_data);
if (!$result) {
throw new Exception("保存失败");
}
}
debug_log('分佣结束', $log_path, $log_dir);
return new MyResponse(EXIT_SUCCESS, '保存成功');
} catch (Exception $e) {
debug_log($e->getMessage(), $log_path, $log_dir);
return new MyResponse(EXIT_ERROR, '分佣失败:' . $e->getMessage());
}
}
}