Files
spacestation/common/libraries/entity/AutoUserCouponEntity.php
T
2025-12-05 14:59:24 +08:00

179 lines
6.8 KiB
PHP

<?php
class AutoUserCouponEntity
{
public $id;
public $sid;
public $userId;
public $couponId;
public $productId;
public $mobile;
public $title;
public $price;
public $timeStart;
public $timeEnd;
public $status;
public $useTime;
public $bizId;
public $unLockBizIds;
private $ci;
public function __construct()
{
$this->ci = &get_instance();
$this->ci->load->model('agent/auto_product_coupon_model');
}
/**
* 获取按钮文字
* @return array
*/
public function getBtText()
{
$result = [
'txt' => Auto_user_coupon_model::STATUS_CN[$this->status],
'status' => $this->status,
];
return $result;
}
/**
* 获取适用门店名称
* @return string
*/
public function getBizName()
{
// $this->ci->load->model('biz/biz_model');
// $this->ci->biz_model->set_db('ssdb');
// $bizIds = explode(',',$this->unLockBizIds);
// $result = '';
// if($bizIds){
// $bizNum = count($bizIds);
// $bizRow = $this->ci->biz_model->get(['id' => $bizIds[0]], 'biz_name');
// $bizName = $bizRow ? $bizRow['biz_name'] : '';
// $result = "适用 {$bizName}";
// if($bizNum>1){
// $result .= "等 {$bizNum}家门店";
// }
// $result .= " 门店";
// }
$result = '适用门店';
return $result;
}
/**
* 核销优惠券
* @return MyResponse
*/
public function useCoupon($formParams)
{
$this->ci->load->model('agent/receiver_order_subsidy_model');
$this->ci->load->model('receiver/receiver_customers_model');
$this->ci->load->model('receiver/receiver_clues_model');
$this->ci->load->model('receiver/order/receiver_orders_model');
$this->ci->receiver_customers_model->set_db('ssdb');
$this->ci->receiver_clues_model->set_db('ssdb');
$this->ci->receiver_orders_model->set_db('ssdb');
$name = $formParams['name'];
$cityId = $formParams['cityId'];
$bizId = $formParams['bizId'];
$brandId = $formParams['brandId'];
$seriesId = $formParams['seriesId'];
$billImg = $formParams['billImg'];
$businessImg = $formParams['businessImg'];
$contractImg = $formParams['contractImg'];
if (!$cityId || !$bizId || !$brandId || !$seriesId || !$billImg || !$name) {
return new MyResponse(EXIT_ERROR, "参数错误");
}
//获取线索和客户信息
$where = [
'app_id' => Receiver_clues_model::APP_ID_ACTIVITY,
'cf_uid' => $this->userId,
// 'out_id' => $this->productId
];
$clueRow = $this->ci->receiver_clues_model->get($where);
$where = [
'rid' => $clueRow['id'],
'biz_id' => $bizId
];
$customersRow = $this->ci->receiver_customers_model->get($where);
$this->ci->db->trans_begin();
try {
$timeStart = strtotime($this->timeStart);
$timeEnd = strtotime($this->timeEnd);
if (time() <= $timeStart) {
throw new Exception('当前优惠券不可用', API_CODE_FAIL);
}
if ($this->status == Auto_user_coupon_model::STATUS_USED) {
throw new Exception('优惠券已使用', API_CODE_FAIL);
}
if (time() > $timeEnd || $this->status == Auto_user_coupon_model::STATUS_EXPIRED) {
throw new Exception('优惠券已过期', API_CODE_FAIL);
}
if ($this->status != Auto_user_coupon_model::STATUS_NOT_USED) {
throw new Exception('当前优惠券不可核销', API_CODE_FAIL);
}
//新增购车订单
$data = [
'clue_id' => $clueRow['id'] ?: 0,
'customer_id' => $customersRow['id'] ?: 0,
'sid' => create_order_no($cityId, 'pingan'),
'name' => $name,
'mobile' => $this->mobile,
'biz_id' => $bizId,
'brand_id' => $brandId,
'series_id' => $seriesId,
'order_time' => date('Y-m-d H:i:s'),
'c_time' => time()
];
$orderId = $this->ci->receiver_orders_model->add($data);
if (!$orderId) {
throw new Exception('新增购车订单失败', API_CODE_FAIL);
}
//新增补贴记录
$data = [
'couponId' => $this->couponId,
'orderId' => $orderId,
'userId' => $this->userId,
'cityId' => $cityId,
'bizId' => $bizId,
'brandId' => $brandId,
'seriesId' => $seriesId,
'billImg' => $billImg,
'businessImg' => $businessImg,
'contractImg' => $contractImg,
'createTime' => date('Y-m-d H:i:s')
];
if (!in_array($clueRow['org_id'], Receiver_clues_model::COMM_ORG_IDS)) {
$data['commStatus'] = Receiver_order_subsidy_model::COMM_STATUS_NOT;
}
$result = $this->ci->receiver_order_subsidy_model->add($data);
if (!$result) {
throw new Exception('新增补贴记录失败', API_CODE_FAIL);
}
//更新核销码状态
$update = [
'orderId' => $orderId,
'useTime' => date('Y-m-d H:i:s'),
'status' => Auto_user_coupon_model::STATUS_USED,
'bizId' => $bizId
];
$result = $this->ci->auto_user_coupon_model->update($update, ['id' => $this->id]);
if (!$result) {
throw new Exception('更新核销码状态失败', API_CODE_FAIL);
}
//跟新客户为订单客户
$this->ci->receiver_customers_model->update(['status' => 2], ['id' => $customersRow['id']]);
$this->ci->db->trans_commit();
//发送短信给店长
$this->ci->load->model('app/licheb/app_licheb_users_model');
$this->ci->app_licheb_users_model->set_db('ssdb');
$this->ci->app_licheb_users_model->sendSmsToManager($bizId, "门店新增了一个订单,请及时到小程序“理车宝-订单”进行处理。");
return new MyRESPONSE(EXIT_SUCCESS, '核销成功', ['orderId' => $orderId]);
} catch (Exception $e) {
$this->ci->db->trans_rollback();
return new MyResponse(EXIT_ERROR, $e->getMessage());
}
}
}