Files
spacestation/common/models/receiver/Receiver_clues_model.php
T

222 lines
7.7 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个月'];
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 => '待挖掘'));
$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 unlock($id, $bizId)
{
$this->load->model('receiver/receiver_customers_model', 'customers_model');
$this->db->trans_begin();
try {
$row = $this->get(['id' => $id]);
if (!$row) {
throw new Exception('数据不存在');
}
$res = $this->clues_model->update(['un_lock' => 1], ['id' => $id]);
if (!$res) {
throw new Exception('更新失败');
}
//添加客户池
/** @var MyResponse $result */
$result = $this->customers_model->addCustomerByCluesId($id);
if (!$result->isSuccess()) {
throw new Exception($result->getMessage());
}
//设置优惠券状态
$result = $this->setCluesCouponStatus($id, $bizId);
if (!$result->isSuccess()) {
throw new Exception($result->getMessage());
}
$this->db->trans_commit();
return new MyResponse(EXIT_SUCCESS, 'success');
} catch (Exception $e) {
$this->db->trans_rollback();
return new MyResponse(EXIT_ERROR, '解锁失败:' . $e->getMessage());
}
}
/**
* @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());
}
}
}