Files
chazhi_admin_broker/app/api/logic/UserCouponLogic.php
2026-03-11 18:24:59 +08:00

239 lines
7.2 KiB
PHP

<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\model\teamaster\Teamaster;
use app\common\model\teastore\TeaStoreRoom;
use app\common\service\ConfigService;
use think\facade\{Db, Config};
use app\common\model\user\{UserCoupon, UserCouponType};
class UserCouponLogic extends BaseLogic
{
/**
* @notes 用户优惠券列表
* @param array $params
* @return array
* @author 胥聪
* @date 2025/10/21 13:50
*/
public static function getUserCoupinList($data,$user_id){
$type_id = $data['type_id'];
$os = "a.type_id =".$type_id;
if($type_id == 3){
$os = '';
}
$result = UserCoupon::alias("a")
->join("user_coupon_type b", "b.id = a.coupon_id", "left")
->where("a.status",0)
->where("a.user_id",$user_id)
->field("a.id,b.title,b.name,b.use_price,b.coupon_price,b.effect_time,a.status")
->where($os)
->select()
->toarray();
$no_use = 0;
$no_use2 = 0;
$all_use = 0;
if($result){
foreach ($result as &$item){
$item['is_use'] = 0; //等于0可用
$all_use++;
if($type_id == 1 ||$type_id == 2){
if($item['use_price']>$data['price']){
$item['is_use'] = 1;
$no_use++;
}
}
if($item['status'] == 1){
$item['is_use'] = 1;
$no_use2++;
}
}
}
$no_use = $no_use+$no_use2;
$count= [
'all_use'=>$all_use,
'no_use' => $no_use,
'yes_use'=>$all_use-$no_use
];
$data = [
'count'=>$count,
'result'=>$result
];
return $data;
}
public static function isCoupin($user_id){
$status = 0;
$user_coupon = UserCoupon::where([
'user_id'=>$user_id,
'coupon_id'=>1
])->find();
if($user_coupon){
$status =1;
}
$data = [
'status'=>$status
];
return $data;
}
public static function receiveCoupon($post,$userId){
try {
$result = UserCouponType::where("id",$post['id'])->find();
$user_coupon = UserCoupon::where([
'user_id'=>$userId,
])->find();
if($post['id'] == 1 &&$user_coupon){
throw new \Exception('此优惠券已领取');
}elseif ($post['id'] == 3||$post['id'] == 4){
$coup_count = UserCoupon::where('user_id',$userId)->whereIn('coupon_id',[3,4])->select();
$count_3 = 0;
$count_4 = 0;
foreach ($coup_count as $item){
if($item['coupon_id'] ==3){
$count_3++;
}elseif ($item['coupon_id']==4){
$count_4++;
}
}
if ($post['id'] == 3&&$count_3 >= 10) {
throw new \Exception('优惠券只能领取10张');
}
if ($post['id'] == 4&&$count_4 >= 10) {
throw new \Exception('优惠券只能领取10张');
}
}
return UserCoupon::create([
'user_id'=>$userId,
'coupon_id'=>$post['id'],
'type_id'=>$result['type_id'],
'dtime'=>date("Y-m-d H:i:s")
]);
return [];
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function shouyeCoupon($userId){
try {
$result = UserCouponType::where("id",1)->find();
$user_coupon = UserCoupon::where([
'user_id'=>$userId,
'coupon_id'=>1
])->find();
if($user_coupon){
throw new \Exception('此优惠券已领取');
}
return UserCoupon::create([
'user_id'=>$userId,
'coupon_id'=>1,
'type_id'=>1,
'dtime'=>date("Y-m-d H:i:s")
]);
return [];
} catch (\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function getOrderCoupinList($data,$user_id){
$expire_time = ConfigService::get("gzh_setting","user_coupon_expire_time");
$threeDays = $expire_time * 24 * 60 * 60; // 转换时间戳
$expireDate = date('Y-m-d', time() + ($expire_time * 24 * 60 * 60));
// 状态为3的为快过期的 数据表中不做记录 只有筛选
$s = "status = 0";
$t = "";
if(isset($data['status'])){
if($data['status']!=0 || $data['status']!="" || $data['status'] != null){
$s = "status = ".$data['status']."";
}elseif($data['status'] == 3){
$t = "dtime <= '".$expireDate."'";
}
}
// 获取过期时间
$result = UserCoupon::with(['userCouponType'])
->where("user_id",$user_id)
->where("type_id",$data['type_id'])
->where($s)
->where($t)
->select()
->toarray();
// 茶艺师价格
// $teamaster_price = Teamaster::where("id",$data['id'])->value("price");
// $all_price = $teamaster_price;
foreach($result as $key=>$value){
// // 可用不可用
// if($all_price >= $value['userCouponType'][0]['use_price']){
// $result[$key]['use'] = 1;
// }else{
// $result[$key]['no_use'] = 2;
// }
// 即将过期
$targetTimestamp = strtotime($value['userCouponType'][0]['effect_time']); // 你的目标时间戳
if (time() < $targetTimestamp && ($targetTimestamp - time()) <= $threeDays) {
$result[$key]['expire'] = 2;
} else {
$result[$key]['expire'] = 1;
}
}
return $result;
}
// 个人中心券列表
public static function coupinList($user_id){
$result = UserCouponType::where("status",0)->select()->toarray();
foreach($result as $key=>$value){
$user_coupon = UserCoupon::where("user_id",$user_id)->where("coupon_id",$value['id'])->select();
$result[$key]['use'] = 0;
if($user_coupon){
$count_3 = 0;
$count_4 = 0;
foreach ($user_coupon as $ucoupon){
if($ucoupon['coupon_id'] == 1){
$result[$key]['use'] = 1;
}elseif($ucoupon['coupon_id'] == 3){
$count_3 ++;
}
elseif($ucoupon['coupon_id'] == 4){
$count_4 ++;
}
}
if ($value['id'] == 3&&$count_3 >= 10) {
$result[$key]['use'] = 1;
}
if ($value['id'] == 4&&$count_4 >= 10) {
$result[$key]['use'] = 1;
}
}
}
return $result;
}
public static function expireCoupin(){
}
}