Files
spacestation/common/models/agent/Auto_user_coupon_model.php
T
2025-12-05 14:59:24 +08:00

203 lines
8.2 KiB
PHP

<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auto_user_coupon_model extends HD_Model
{
//状态 0资格审核中 1未使用 2已使用 21已过期
const STATUS_AUDITING = 0;
const STATUS_NOT_USED = 1;
const STATUS_USED = 2;
const STATUS_EXPIRED = 21;
const STATUS_CN = [
self::STATUS_AUDITING => '发券中',
self::STATUS_NOT_USED => '立即使用',
self::STATUS_USED => '已使用',
self::STATUS_EXPIRED => '已过期',
];
private $table_name = 'lc_auto_user_coupon';
public function __construct()
{
parent::__construct($this->table_name, 'default');
}
/**
* 用户获取优惠券
* @param $appId
* @param $userId
* @param $couponId
* @param $mobile
* @param $cityId
* @return MyResponse
*/
public function getCoupon($appId, $userId, $couponId, $mobile, $cityId)
{
$this->load->model('area_model');
$this->load->model('agent/auto_user_model', 'autoUserModel');
$this->load->model('agent/organization/organization_model', 'organizationModel');
$this->load->model('agent/auto_product_model');
$this->load->model('agent/auto_product_coupon_model', 'productCoupon');
$this->load->model('receiver/receiver_enroll_model');
$this->load->model('receiver/receiver_clues_model');
$this->receiver_clues_model->set_db('ssdb');
$this->receiver_enroll_model->set_db('ssdb');
try {
$coupon = $this->productCoupon->get(['id' => $couponId]);
if (!$coupon) {
throw new Exception('优惠券不存在');
}
$areaRow = [];
!$cityId && $cityId = 350200;
if ($cityId) {
$areaRow = $this->area_model->get(['city_id' => $cityId]);
}
$where = [
'userId' => $userId,
'couponId' => $couponId,
];
$row = $this->get($where);
$sid = create_order_no($cityId, 'pingan');
$product = $this->auto_product_model->get(['id' => $coupon['product_id']]);
$user = $this->autoUserModel->get(['id' => $userId]);
$teamCf = $this->organizationModel->getCfIdByTeamId($user['orgTeamId']);
if ($row) {
throw new Exception('优惠券已领取');
}
$data = [
'sid' => $sid,
'userId' => $userId,
'productId' => $coupon['product_id'],
'couponId' => $couponId,
'mobile' => $mobile,
'title' => $coupon['title'],
'price' => $coupon['price'],
'timeStart' => $coupon['timeStart'],
'timeEnd' => $coupon['timeEnd'],
];
$res = $this->add($data);
if (!$res) {
throw new Exception('领取失败');
}
$cf_id = $teamCf['cfId'] ?: 0;
$cf2_id = $teamCf['cf2Id'] ?: 0;
if ($user['cfrom'] == Auto_user_model::C_FROM_APP) { //来源app
$cf_id = 38;
$cf2_id = 43;
}
//增加留资记录
$enrollData = [
'sid' => $sid,
'mobile' => $mobile,
'cf_id' => $cf_id,
'cf2_id' => $cf2_id,
'out_id' => $product['id'],
'c_time' => time()
];
$enrollData['brand_id'] = $product['brandId'] ?: 0;
$enrollData['series_id'] = $product['seriesId'] ?: 0;
$this->receiver_enroll_model->add($enrollData);
//写入线索池
$cluesRow = $this->receiver_clues_model->get(['mobile' => $mobile]);
if (!$cluesRow) {
$data = [
'sid' => create_order_no($cityId, 'pingan'),
'mobile' => $mobile,
'en_time' => date('Y-m-d H:i:s'),
'out_id' => $product['id'],
'app_id' => $appId,
'cf_uid' => $userId,
'cf_id' => $cf_id,
'cf2_id' => $cf2_id,
'c_time' => time(),
];
$data['brand_id'] = $product['brandId'] ?: 0;
$data['series_id'] = $product['seriesId'] ?: 0;
$data['org_id'] = $user['orgId'] ?: 0;
$data['pingan_user_id'] = $user['pinganUserId'] ?: 0;
$data['team_id'] = $user['teamId'] ?: 0;
$data['dep_id'] = $user['depId'] ?: 0;
$data['area_id'] = $user['areaId'] ?: 0;
$data['province_id'] = $areaRow['province_id'] ?: 0;
$data['city_id'] = $areaRow['city_id'] ?: 0;
$data['county_id'] = $areaRow['county_id'] ?: 0;
if ($data['province_id'] == 460000) { //海南省
$data['belong_id'] = 39;
} else if ($data['province_id'] == 350000) { //福建
$data['belong_id'] = 10;
}
$this->receiver_clues_model->add($data);
} else {
$this->receiver_clues_model->update(['en_time' => date('Y-m-d H:i:s')], ['id' => $cluesRow['id']]);
}
$result = ['id' => $res, 'product_id' => $coupon['product_id']];
return new MyResponse(EXIT_SUCCESS, '操作成功', $result);
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
/**
* 给指定线索发送优惠券
* @param $cluesId
* @param $couponId
* @return MyResponse
*/
public function sendCoupon($cluesId, $couponId)
{
$this->load->model('agent/auto_product_model');
$this->load->model('agent/auto_product_coupon_model');
$this->load->model('receiver/receiver_enroll_model');
$this->load->model('receiver/receiver_clues_model');
$this->receiver_clues_model->set_db('ssdb');
$this->receiver_enroll_model->set_db('ssdb');
try {
$cluesRow = $this->receiver_clues_model->get(['id' => $cluesId]);
if (!$cluesRow) {
throw new Exception('线索不存在');
}
$coupon = $this->auto_product_coupon_model->get(['id' => $couponId]);
if (!$coupon) {
throw new Exception('优惠券不存在');
}
$where = ['userId' => $cluesRow['cf_uid'], 'couponId' => $couponId];
$row = $this->get($where);
if ($row) {
throw new Exception('优惠券已领取');
}
$sid = create_order_no($cluesRow['city_id'], 'pingan');
$data = [
'sid' => $sid,
'userId' => $cluesRow['cf_uid'],
'productId' => $coupon['product_id'],
'couponId' => $couponId,
'mobile' => $cluesRow['mobile'],
'title' => $coupon['title'],
'price' => $coupon['price'],
'timeStart' => $coupon['timeStart'],
'timeEnd' => $coupon['timeEnd'],
];
$res = $this->add($data);
if (!$res) {
throw new Exception('发送失败');
}
$product = $this->auto_product_model->get(['id' => $coupon['product_id']]);
//增加留资记录
$enrollData = [
'sid' => $sid,
'mobile' => $cluesRow['mobile'],
'cf_id' => $cluesRow['cf_id'],
'cf2_id' => $cluesRow['cf2_id'],
'out_id' => $coupon['product_id'],
'c_time' => time()
];
$enrollData['brand_id'] = $product['brandId'] ?: 0;
$enrollData['series_id'] = $product['seriesId'] ?: 0;
$this->receiver_enroll_model->add($enrollData);
return new MyResponse(EXIT_SUCCESS, '操作成功');
} catch (Exception $e) {
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}