coupon-admin/app/repository/CouponRepository.php

124 lines
3.3 KiB
PHP
Raw Normal View History

2021-11-18 09:57:04 +00:00
<?php
2021-12-02 10:34:44 +00:00
2021-11-18 09:57:04 +00:00
namespace app\repository;
2021-11-25 10:11:50 +00:00
use app\model\Business;
2021-11-24 08:34:56 +00:00
use app\model\Coupon;
2021-11-18 09:57:04 +00:00
use app\model\CouponMain;
2021-11-25 10:11:50 +00:00
use app\model\CouponType;
2021-12-02 10:34:44 +00:00
use app\model\UsingRule;
2021-11-18 09:57:04 +00:00
use app\service\Repository;
2021-12-17 06:44:05 +00:00
use app\traits\CouponBillTrait;
2021-12-06 10:56:23 +00:00
use app\traits\CouponMainTrait;
use Exception;
2021-11-29 08:34:25 +00:00
use think\Collection;
2021-11-25 10:11:50 +00:00
use think\Db;
2021-11-25 10:56:30 +00:00
2021-11-18 09:57:04 +00:00
use think\Model;
/**
2022-03-31 06:42:56 +00:00
* 签到券 相关操作
2021-11-18 09:57:04 +00:00
*
* Class CouponRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class CouponRepository extends Repository
{
2021-12-06 10:56:23 +00:00
use CouponMainTrait;
2021-12-17 06:44:05 +00:00
use CouponBillTrait;
2021-11-18 09:57:04 +00:00
/**
2022-03-31 06:42:56 +00:00
* 签到券持有信息列表
2021-11-18 09:57:04 +00:00
*
2021-11-24 08:34:56 +00:00
* @param $id
* @param $keyword
* @param $page
* @param $size
2021-11-18 09:57:04 +00:00
* @return array
2021-11-24 08:34:56 +00:00
* @throws \Exception
2021-11-18 09:57:04 +00:00
*/
2021-11-24 08:34:56 +00:00
public function couponMainHasList($id, $keyword, $page, $size)
2021-11-18 09:57:04 +00:00
{
2021-12-22 08:19:36 +00:00
return Coupon::findList(["coupon_id" => $id], ["*"], $page, $size, function ($q) use ($keyword) {
2021-11-24 08:34:56 +00:00
if (!empty($keyword)) {
return $q::hasWhere('account', function ($q) use ($keyword) {
2022-02-16 09:53:58 +00:00
$q->where('nick_name', 'like', "%" . $keyword . "%")->field("nick_name,avatar_url,user_code,mobile");
2021-11-24 08:34:56 +00:00
});
} else {
return $q->with(["account" => function ($query) {
2022-02-16 09:53:58 +00:00
$query->field("nick_name,avatar_url,user_code,mobile");
2021-11-24 08:34:56 +00:00
}]);
}
}, ["id" => "desc"]);
2021-11-18 09:57:04 +00:00
}
2021-11-25 10:11:50 +00:00
/**
2021-11-29 08:34:25 +00:00
* 获取优惠卷类型
2021-11-25 10:11:50 +00:00
*
2021-11-29 08:34:25 +00:00
* @param array $fields
* @param array $order
* @return Collection
2021-12-02 10:34:44 +00:00
*/
2021-11-29 08:34:25 +00:00
public function getCouponTypeAll(array $fields = [], array $order = ["id" => "desc"])
2021-11-25 10:11:50 +00:00
{
2021-11-29 08:34:25 +00:00
try {
return CouponType::order($order)->field($fields)->select();
} catch (\Exception $e) {
return new Collection();
}
2021-11-25 10:11:50 +00:00
}
/**
2022-03-31 06:42:56 +00:00
* 发布签到券 扣除商家余额
2021-11-25 10:11:50 +00:00
* @param $data
* @param $totalMoney
2021-12-02 10:34:44 +00:00
* @param $usingRule 使用规则
2021-11-18 09:57:04 +00:00
*/
2022-01-19 05:40:46 +00:00
public function releaseCouponMain($data)
2021-11-18 09:57:04 +00:00
{
2022-03-31 06:42:56 +00:00
//创建签到券根表
2021-12-02 10:34:44 +00:00
$couponMain = CouponMain::create($data);
2022-03-31 06:42:56 +00:00
//创建签到券使用规则
2022-01-19 05:40:46 +00:00
//$usingRule["coupon_id"] = $couponMain->id;
//UsingRule::create($usingRule);
2021-12-14 09:34:27 +00:00
//Business::where("code", $data["business_code"])->dec("balance", $totalMoney)->update();
2021-11-18 09:57:04 +00:00
}
/**
* 查询商家发布的优惠卷列表
*
* @param array $where
* @param array $fields
* @param int $page
* @param int $size
* @param callable|null $call
* @param array $sortOrder
* @return array
* @throws Exception
*/
2021-12-02 10:34:44 +00:00
public function findCouponMainList(array $where, array $fields = [], int $page = 1, int $size = 0, callable $call = null, array $sortOrder = []): array
{
return CouponMain::findList($where, $fields, $page, $size, $call, $sortOrder);
}
2021-12-06 10:56:23 +00:00
/**
2022-03-31 06:42:56 +00:00
* 写入一个领取签到券
2021-12-06 10:56:23 +00:00
* @param array $data
* @return Coupon|Model
*/
public function receiveCoupon(array $data)
{
return Coupon::create($data);
}
2022-04-07 05:59:03 +00:00
/**
* 根据商家id修改优惠券
* @param array $data
* @return Coupon|Model
*/
2022-04-07 06:01:13 +00:00
public function updateCouponMainByBusinessCode(string $businessCode,array $data)
2022-04-07 05:59:03 +00:00
{
return CouponMain::where("business_code",$businessCode)->update($data);
}
2021-11-18 09:57:04 +00:00
}