商家优惠券管理初始化
parent
3fc33392f1
commit
49ba74b186
|
@ -386,6 +386,28 @@ class Business extends Base
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* 商家详情
|
||||
* */
|
||||
public function getShopInfo()
|
||||
{
|
||||
$accountId = $this->request->user['user_id'] ?? 0;
|
||||
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
|
||||
return $q->with(['business', 'parent']);
|
||||
});
|
||||
if(empty($account)){
|
||||
return $this->json(6001,"登录失效");
|
||||
}
|
||||
if ($account->type == Account::type_consumer) {
|
||||
return $this->json(4001, "您不是商家");
|
||||
}
|
||||
|
||||
if (isset($account->business) && $account->business) {
|
||||
return $this->json(0, "success",["business"=>$account->business]);
|
||||
}
|
||||
return $this->json(4001, "获取商家信息错误");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -2,11 +2,15 @@
|
|||
namespace app\controller\api;
|
||||
|
||||
use app\exception\RepositoryException;
|
||||
use app\model\Account;
|
||||
use app\model\CouponMain;
|
||||
use app\model\Score;
|
||||
use app\repository\AccountRepository;
|
||||
use app\repository\CouponRepository;
|
||||
use app\validate\CouponRelease;
|
||||
use app\validate\CouponUsingRule;
|
||||
use think\Exception;
|
||||
use think\facade\Config;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
|
@ -17,9 +21,7 @@ use think\facade\Log;
|
|||
*/
|
||||
class Coupon extends Base
|
||||
{
|
||||
protected $noNeedLogin = [
|
||||
|
||||
];
|
||||
protected $noNeedLogin = [];
|
||||
|
||||
/**
|
||||
* 我的优惠卷列表
|
||||
|
@ -178,4 +180,229 @@ class Coupon extends Base
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布优惠券
|
||||
* */
|
||||
public function add()
|
||||
{
|
||||
$accountId = $this->request->user['user_id'] ?? 0;
|
||||
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
|
||||
return $q->with(['business'=>function($q){
|
||||
$q->lock(true);
|
||||
}, 'parent']);
|
||||
});
|
||||
if(empty($account)){
|
||||
return $this->json(6001,"登录失效");
|
||||
}
|
||||
if ($account->type == Account::type_consumer) {
|
||||
return $this->json(4001, "您不是商家");
|
||||
}
|
||||
|
||||
if (!isset($account->business) || empty($account->business)) {
|
||||
return $this->json(4001, "商家信息错误");
|
||||
}
|
||||
$data = input();
|
||||
|
||||
Config::load("extra/distribution_proportion","distribution_proportion");
|
||||
$distributionProportion = config("distribution_proportion");
|
||||
|
||||
$totalC = $distributionProportion['agency'] + $distributionProportion['admin'] + $distributionProportion['consumer'];
|
||||
if ($totalC != 100) {
|
||||
return $this->json(5002, "系统设置分配比例总和不等于100,不能发布");
|
||||
}
|
||||
|
||||
//验证通过 不管是商家还是工作人员 都可以发布优惠券
|
||||
$couponMain = [
|
||||
"name" => $data['name'] ?? '',
|
||||
"business_code" => $account->business->code,
|
||||
"money" => $data['money'] ?? 0,
|
||||
"type" => $data['type'] ?? 0,
|
||||
"count" => $data['count'] ?? 0,
|
||||
"type_name" => $data['type_name'] ?? '',
|
||||
"start_time" => $data['start_time'] ?? '',
|
||||
"end_time" => $data['end_time'] ?? '',
|
||||
"deduction_money" => $data['deduction_money'] ?? '',
|
||||
"image_url" => $data['image_url'] ?? '',
|
||||
"intro" => $data['intro'] ?? '',
|
||||
"status" => CouponMain::status_on,
|
||||
"on_shelf" => CouponMain::on_shelf_off,//默认下架 后天审核上架
|
||||
"commission_agency" => $distributionProportion['agency'],
|
||||
"commission_admin" => $distributionProportion['admin'],
|
||||
"commission_consumer" => $distributionProportion['consumer'],
|
||||
];
|
||||
|
||||
$usingRule = input("using_rule/a");
|
||||
|
||||
$validate = new CouponRelease();
|
||||
if (!$validate->check($couponMain)) {
|
||||
return $this->json(4001, $validate->getError());
|
||||
}
|
||||
|
||||
$usingRuleValidate = new CouponUsingRule();
|
||||
if (!$usingRuleValidate->check($usingRule)) {
|
||||
return $this->json(4001, $usingRuleValidate->getError());
|
||||
}
|
||||
//验证通过
|
||||
$couponMain['business_type'] = $account->business['type'];
|
||||
$couponMain['business_name'] = $account->business['business_name'];
|
||||
$couponMain['lng'] = $account->business['lng'];
|
||||
$couponMain['lat'] = $account->business['lat'];
|
||||
$couponMain['create_time'] = date("Y-m-d H:i:s");
|
||||
|
||||
//保留两位小数
|
||||
$couponMain['money'] = floor($couponMain['money'] * 100) / 100;
|
||||
$totalMoney = $couponMain['money'] * $couponMain['count'];
|
||||
|
||||
if ($account->business["balance"] < $totalMoney) {
|
||||
return $this->json(4001, '商家余额不足');
|
||||
}
|
||||
|
||||
Db::startTrans();
|
||||
try {
|
||||
CouponRepository::getInstance()->releaseCouponMain($couponMain, $totalMoney, $usingRule);
|
||||
Db::commit();
|
||||
return $this->json();
|
||||
} catch (RepositoryException $e) {
|
||||
Db::rollback();
|
||||
return $this->json(5001, "发布失败" . $e->getMessage());
|
||||
} catch (\think\Exception $e) {
|
||||
Db::rollback();
|
||||
return $this->json(5002, "发布失败" . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 商家管理优惠券
|
||||
* */
|
||||
public function getPageList()
|
||||
{
|
||||
$accountId = $this->request->user['user_id'] ?? 0;
|
||||
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
|
||||
return $q->with(['business', 'parent']);
|
||||
});
|
||||
if(empty($account)){
|
||||
return $this->json(6001,"登录失效");
|
||||
}
|
||||
if ($account->type == Account::type_consumer) {
|
||||
return $this->json(4001, "您不是商家");
|
||||
}
|
||||
|
||||
if (!isset($account->business) || empty($account->business)) {
|
||||
return $this->json(4001, "商家信息错误");
|
||||
}
|
||||
|
||||
$page = $this->request->param('page/d', 1);
|
||||
$size = $this->request->param('rows/d', 10);
|
||||
$page = $page < 1 ? 1 : $page;
|
||||
$size = $size < 1 ? 10 : $size;
|
||||
$keyword = $this->request->param('key/s',"");
|
||||
$businessCode = $account->business_code;
|
||||
|
||||
try {
|
||||
$whereMap = [];
|
||||
$sortOrder = ['sort' => 'desc'];
|
||||
$whereMap[] = ["business_code" ,"=", $businessCode ];
|
||||
if(!empty($keyword)){
|
||||
$whereMap[] = ["name" ,"like", "%{$keyword}%" ];
|
||||
}
|
||||
$res = CouponRepository::getInstance(new CouponMain())->findList($whereMap, [], $page, $size,function ($q){
|
||||
return $q->with(["usingRule","couponType"]);
|
||||
}, $sortOrder);
|
||||
$time = time();
|
||||
$res['list'] ->each(function ($item) use($time){
|
||||
//已使用张数
|
||||
$item->usingCount = CouponRepository::getInstance()->getCouponMainUsingCount($item->id);
|
||||
if(strtotime($item->start_time) <= $time && strtotime($item->end_time) >= $time){
|
||||
$item->conduct = true;
|
||||
}else{
|
||||
$item->conduct = false;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return $this->json(0, 'success', $res["list"]);
|
||||
} catch (RepositoryException | \Exception $e) {
|
||||
echo $e->getMessage();
|
||||
return $this->json(5001, '优惠卷查询失败!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 优惠券的领取 使用记录列表
|
||||
* */
|
||||
public function getShopCouponList()
|
||||
{
|
||||
$accountId = $this->request->user['user_id'] ?? 0;
|
||||
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
|
||||
return $q->with(['business', 'parent']);
|
||||
});
|
||||
if(empty($account)){
|
||||
return $this->json(6001,"登录失效");
|
||||
}
|
||||
if ($account->type == Account::type_consumer) {
|
||||
return $this->json(4001, "您不是商家");
|
||||
}
|
||||
|
||||
if (!isset($account->business) || empty($account->business)) {
|
||||
return $this->json(4001, "商家信息错误");
|
||||
}
|
||||
|
||||
$couponMainId = input("couponId/d");
|
||||
|
||||
$couponMain = CouponMain::findById($couponMainId);
|
||||
|
||||
if(empty($couponMain)){
|
||||
return $this->json(4001, "优惠券不存在");
|
||||
}
|
||||
if($couponMain->business_code != $account->business->code){
|
||||
return $this->json(4001, "优惠券参数信息错误");
|
||||
}
|
||||
|
||||
$page = $this->request->param('page/d', 1);
|
||||
$size = $this->request->param('rows/d', 10);
|
||||
$type = $this->request->param('type/s', 10);
|
||||
$page = $page < 1 ? 1 : $page;
|
||||
$size = $size < 1 ? 10 : $size;
|
||||
$sortOrder = ["id"=>"desc"];
|
||||
$whereMap = [
|
||||
["coupon_id","=",$couponMainId]
|
||||
];
|
||||
switch ($type) {
|
||||
case 'all':
|
||||
// 全部持有优惠券
|
||||
$sortOrder = [ 'end_time' => 'asc'];
|
||||
break;
|
||||
case 'notUsed':
|
||||
// 未使用(包含已过期)
|
||||
$whereMap[] = ['is_verificated', '=', self::BOOL_FALSE];
|
||||
$sortOrder = ['end_time' => 'asc'];
|
||||
break;
|
||||
case 'normal':
|
||||
// 未使用且未过期
|
||||
$whereMap[] = ['is_verificated', '=', self::BOOL_FALSE];
|
||||
$whereMap[] = ['end_time', '< TIME', date('Y-m-d H:i:s')];
|
||||
break;
|
||||
case 'used':
|
||||
// 已使用
|
||||
$whereMap[] = ['is_verificated', '=', self::BOOL_TRUE];
|
||||
$sortOrder = ['verificate_time' => 'desc'];
|
||||
break;
|
||||
}
|
||||
$field = ["is_verificated","received_time","verificate_time","consumer_code"];
|
||||
$data = CouponRepository::getInstance()->findList($whereMap,$field,$page,$size,function($q){
|
||||
return $q->with(["account"=>function($q){
|
||||
$q->field(["nick_name","avatar_url","gender","user_code"]);
|
||||
}]);
|
||||
},$sortOrder);
|
||||
|
||||
//未使用
|
||||
$data["allNum"] = $data["total"];
|
||||
$data["unUsedNum"] = CouponRepository::getInstance()->getModel()->where(['is_verificated' => self::BOOL_FALSE,"coupon_id"=>$couponMainId])->count();
|
||||
$data["usedNum"] = CouponRepository::getInstance()->getModel()->where(['is_verificated' => self::BOOL_TRUE ,"coupon_id"=>$couponMainId])->count();
|
||||
|
||||
return $this->json(0,"success",$data);
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -35,6 +35,8 @@ class CouponMain extends Base
|
|||
public static function onAfterInsert( $obj)
|
||||
{
|
||||
$obj->sort = $obj->id;
|
||||
$obj->using_count = $obj->count;
|
||||
$obj->received_count = 0;
|
||||
$obj->save();
|
||||
}
|
||||
}
|
|
@ -37,4 +37,16 @@ trait CouponMainTrait
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计这个优惠券已经使用了多少张了
|
||||
* @param $couponMainId
|
||||
* @return int
|
||||
*/
|
||||
public function getCouponMainUsingCount($couponMainId)
|
||||
{
|
||||
return Coupon::where("coupon_id",$couponMainId)
|
||||
->where("is_verificated",Coupon::is_verificated_on)
|
||||
->count();
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ class CouponUsingRule extends Validate
|
|||
protected $rule = [
|
||||
'day_start_time|每日时段限制' => 'require',
|
||||
'day_end_time|每日时段限制' => 'require',
|
||||
'cycle|周期' => 'require|array',
|
||||
'cycle|周期' => 'require|array|length:1,7',
|
||||
'day_total|每日领取总数' => 'require|number',
|
||||
'person_day_total|单人日限制' => 'require|number',
|
||||
'person_total|单人总限制' => 'require|number',
|
||||
|
|
Loading…
Reference in New Issue