Files
hyperf_service/app/Service/Admin/Coupon/TemplateService.php
2025-03-05 16:26:43 +08:00

118 lines
3.5 KiB
PHP

<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\Coupon;
use App\Constants\Common\CouponCode;
use App\Exception\ErrException;
use App\Model\CouponTemplate;
use App\Service\Admin\BaseService;
use Hyperf\Di\Annotation\Inject;
class TemplateService extends BaseService
{
/**
* @var CouponTemplate
*/
#[Inject]
protected CouponTemplate $couponTemplateModel;
/**
* @return array
*/
public function handle(): array
{
$limit = (int)$this->request->input('limit', 10);
$list = $this
->couponTemplateModel
->when($searchName = $this->request->input('query_name'), function ($query) use ($searchName) {
$query->where('name', 'like', "$searchName%");
})
->when($status = $this->request->input('query_status'), function ($query) use ($status) {
$query->where('id', $status);
})
->paginate($limit)->toArray();
return $this->return->success('success',$list);
}
/**
* @return array
* @throws ErrException
*/
public function add(): array
{
$insertModel = new CouponTemplate();
$insertModel->name = $this->request->input('name');
$insertModel->coupon_type = $this->request->input('coupon_type');
$insertModel->amount = $this->request->input('amount','0.00');
$insertModel->ratio = $this->request->input('ratio','0.00');
$insertModel->is_admin_edit = CouponCode::IS_ADMIN_EDIT;
$insertModel->status = CouponCode::COUPON_TEMPLATE_STATUS_NORMAL;
if (!$insertModel->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
* @throws ErrException
*/
public function edit(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
if (!$info) throw new ErrException('优惠券模板不存在');
if ($info->is_admin_edit != CouponCode::IS_ADMIN_EDIT) throw new ErrException('该优惠券模板不允许修改,请联系运维或者后端工程师在不影响程序的情况下进行修改');
$info->name = $this->request->input('name');
$info->coupon_type = $this->request->input('coupon_type');
$info->amount = $this->request->input('amount','0.00');
$info->ratio = $this->request->input('ratio','0.00');
if (!$info->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
*/
public function changeStatus(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
if (!$info) throw new ErrException('优惠券模板不存在');
$info->status = $info->status == CouponCode::COUPON_TEMPLATE_STATUS_NORMAL ? CouponCode::COUPON_TEMPLATE_STATUS_ENABLE : CouponCode::COUPON_TEMPLATE_STATUS_NORMAL;
if (!$info->save()) throw new ErrException('保存优惠券模板失败');
return $this->return->success();
}
/**
* @return array
*/
public function info(): array
{
$id = (int)$this->request->input('id');
$info = $this->couponTemplateModel->getInfoById($id);
return $this->return->success('success',$info->toArray());
}
}