coupon-admin/app/repository/CouponRepository.php

124 lines
3.3 KiB
PHP

<?php
namespace app\repository;
use app\model\Business;
use app\model\Coupon;
use app\model\CouponMain;
use app\model\CouponType;
use app\model\UsingRule;
use app\service\Repository;
use app\traits\CouponBillTrait;
use app\traits\CouponMainTrait;
use Exception;
use think\Collection;
use think\Db;
use think\Model;
/**
* 签到券 相关操作
*
* Class CouponRepository
* @package app\repository
* @method self getInstance(Model $model = null) static
*/
class CouponRepository extends Repository
{
use CouponMainTrait;
use CouponBillTrait;
/**
* 签到券持有信息列表
*
* @param $id
* @param $keyword
* @param $page
* @param $size
* @return array
* @throws \Exception
*/
public function couponMainHasList($id, $keyword, $page, $size)
{
return Coupon::findList(["coupon_id" => $id], ["*"], $page, $size, function ($q) use ($keyword) {
if (!empty($keyword)) {
return $q::hasWhere('account', function ($q) use ($keyword) {
$q->where('nick_name', 'like', "%" . $keyword . "%")->field("nick_name,avatar_url,user_code,mobile");
});
} else {
return $q->with(["account" => function ($query) {
$query->field("nick_name,avatar_url,user_code,mobile");
}]);
}
}, ["id" => "desc"]);
}
/**
* 获取优惠卷类型
*
* @param array $fields
* @param array $order
* @return Collection
*/
public function getCouponTypeAll(array $fields = [], array $order = ["id" => "desc"])
{
try {
return CouponType::order($order)->field($fields)->select();
} catch (\Exception $e) {
return new Collection();
}
}
/**
* 发布签到券 扣除商家余额
* @param $data
* @param $totalMoney
* @param $usingRule 使用规则
*/
public function releaseCouponMain($data)
{
//创建签到券根表
$couponMain = CouponMain::create($data);
//创建签到券使用规则
//$usingRule["coupon_id"] = $couponMain->id;
//UsingRule::create($usingRule);
//Business::where("code", $data["business_code"])->dec("balance", $totalMoney)->update();
}
/**
* 查询商家发布的优惠卷列表
*
* @param array $where
* @param array $fields
* @param int $page
* @param int $size
* @param callable|null $call
* @param array $sortOrder
* @return array
* @throws Exception
*/
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);
}
/**
* 写入一个领取签到券
* @param array $data
* @return Coupon|Model
*/
public function receiveCoupon(array $data)
{
return Coupon::create($data);
}
/**
* 根据商家id修改优惠券
* @param array $data
* @return Coupon|Model
*/
public function updateCouponMainByBusinessCode(string $businessCode,array $data)
{
return CouponMain::where("business_code",$businessCode)->update($data);
}
}