feat : auto coupon
This commit is contained in:
@@ -10,12 +10,89 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\Coupon;
|
||||
|
||||
use App\Constants\Common\CouponCode;
|
||||
use App\Model\CouponDispenseLog;
|
||||
use App\Model\CouponDispenseUser;
|
||||
use App\Model\CouponTemplate;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class DispenseService extends BaseService
|
||||
{
|
||||
public function handle()
|
||||
/**
|
||||
* @var CouponDispenseLog
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseLog $couponDispenseLogModel;
|
||||
|
||||
/**
|
||||
* @var CouponDispenseUser
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponDispenseUser $couponDispenseUserModel;
|
||||
|
||||
/**
|
||||
* @var CouponTemplate
|
||||
*/
|
||||
#[Inject]
|
||||
protected CouponTemplate $couponTemplateModel;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
//todo Write logic
|
||||
$info = $this->couponDispenseLogModel->getInfoById((int)$this->request->input('id'));
|
||||
|
||||
if (empty($info)) throw new Exception('数据不存在');
|
||||
|
||||
$templateInfo = $this->couponTemplateModel->getInfoById($info->coupon_template_id);
|
||||
|
||||
$subData = [];
|
||||
if ($info->appoint_group != CouponCode::DISPENSE_APPOINT_GROUP_ALL_PEOPLE) {
|
||||
$userInfo = $this->couponDispenseUserModel->where('coupon_dispense_id',$info->id)->select(['receive_count','is_receive','user_id'])->get();
|
||||
|
||||
if (!empty($userInfo)) $subData = $userInfo->toArray();
|
||||
}
|
||||
|
||||
return $this->return->success('success',[
|
||||
'info' => $info,
|
||||
'template_info' => $templateInfo,
|
||||
'sub_data' => $subData,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function list(): array
|
||||
{
|
||||
$limit = (int)$this->request->input('limit', 10);
|
||||
|
||||
$list = $this
|
||||
->couponDispenseLogModel
|
||||
->when($userId = $this->request->input('query_user_id'), function ($query) use ($userId) {
|
||||
$query->whereIn('id', $this->couponDispenseUserModel->where('user_id', $userId)->pluck('coupon_dispense_id')->toArray());
|
||||
})
|
||||
->orderBy('id', 'desc')
|
||||
->paginate($limit,[
|
||||
'title','coupon_name','coupon_template_id','total_count','receive_count','item_count','appoint_group','claim_rule','claim_value','appoint_value','validity_time_type','validity_time_value'
|
||||
])->toArray();
|
||||
|
||||
if (empty($list['data'])) return $this->return->success('success', $list);
|
||||
|
||||
|
||||
$templateIds = array_column($list['data'],'coupon_template_id');
|
||||
$templateList = $this->couponTemplateModel->getDataByIds($templateIds);
|
||||
|
||||
foreach ($list['data'] as &$item) {
|
||||
$item['template_info'] = $templateList[$item['coupon_template_id']];
|
||||
$item['appoint_value'] = json_decode($item['appoint_value'],true);
|
||||
$item['validity_time_value'] = json_decode($item['validity_time_value'],true);
|
||||
}
|
||||
|
||||
return $this->return->success('success', $list);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user