目录
前言
之前我写过一篇如何少写PHP “烂”代码 https://blog.fastrun.cn/2018/06/13/1-9/
感觉很多新人对此不太理解。今天以打卡功能为例,去讲解其中的奥秘。那篇文章讲过代码开发的过程中分几种类型。
增删改的需求
Route -> Controller -> Service -> Action
查的需求
Route -> Controller -> Service -> Repository
经过多次实际开发验证后,发现Repository完全是多次一举。所以在这里更正下,取消Repository。
Route -> Controller -> Service
打卡系统逻辑架构图
需求是这样的,用户每天打卡获得积分,积分计入用户账户,并且需记录用户积分的获取及消费情况。如图所示,请求到控制器后,通过控制器去调用服务,服务又调用创建用户打卡模块完成打开,在用户打卡过程中对用户账户积分进行变更及记录用户积分获取记录。
数据表结构
打卡数据表
CREATE TABLE `member_attendance` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL COMMENT '用户编码',
`status` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0' COMMENT '1签到成功',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
用户钱包表
CREATE TABLE `wallet` (
`user_id` bigint(20) NOT NULL COMMENT '用户标示',
`balance` decimal(12,2) NOT NULL COMMENT '钱包余额',
`integral` decimal(12,2) NOT NULL DEFAULT '0',
`add_time` int(11) NOT NULL COMMENT '添加时间',
`update_time` int(11) NOT NULL COMMENT '更改时间',
UNIQUE KEY `wallet_user_id_unique` (`user_id`),
KEY `wallet_user_id_index` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
用户积分交易记录表
CREATE TABLE `member_integral_detail` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`member_id` int(11) NOT NULL COMMENT '用户编码',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '来源或消费',
`integral` decimal(12,2) NOT NULL DEFAULT '0.00' COMMENT '积分数',
`type` tinyint(4) NOT NULL DEFAULT '0' COMMENT '类型 0收入 -1支出',
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
具体业务实现
Route
$api->post ('user/attendance','UserController@attendance');
MemberController
public function attendance()
{
$result = $this->userService->attendance ($this->request);
if ($result) {
return $this->response->array (Response::return (200, '打卡成功'));
}
return $this->response->array (Response::return (0, "打卡失败或已打卡"));
}
MemberService
public function attendance($request)
{
return (new CreateUserAttendance())->execute ($request);
}
CreateUserAttendance
public function issetToday($userId)
{
$result = MemberAttendance::where ([
['member_id', '=', $userId],
])
->whereDate ('created_at', date ('Y-m-d', time ()))
->exists ();
return $result;
}
// -------------------- 上述是下方issetToday方法,写在MemberModel中
class CreateUserAttendance
{
public function execute($data)
{
if ((new MemberAttendance())->issetToday ($data->user_id)) {
return false;
}
$models = new MemberAttendance();
$models->member_id = $data->user_id;
$models->status = (string)"1";
$result = $models->save ();
if ($result) {
(new CreateUserIntegralDetail())->execute ($data->user_id, '打卡', 10, 0);
return true;
}
return false;
}
}
CreateUserIntegralDetail
interface integralDetail
{
public function execute($userId, $title, $integral, $type);
}
class CreateUserIntegralDetail extends UpdateUserWalletIntegral implements integralDetail
{
public function execute($userId, $title, $integral, $type)
{
parent::exec ($userId, $integral, $type);
$models = new MemberIntegralDetail();
$models->member_id = $userId;
$models->title = $title;
$models->integral = $integral;
$models->type = $type;
return $models->save ();
}
}
上述代码继承了更新用户积分的动作,在每次打卡成功后,我们调用父类方法直接更新用户积分。
UpdateUserWalletIntegral
class UpdateUserWalletIntegral
{
public function exec($userId, $integral, $type)
{
if ($type == 0) {
Wallet::where (['user_id', '=', $userId])->increment ('integral', $integral);
} else {
Wallet::where (['user_id', '=', $userId])->decrement ('integral', $integral);
}
}
}
致谢
感谢你看到这里,希望本篇文章可以帮到你。有什么问题可在下方评论区留言。谢谢🙏
原创文章,作者:CrazyCodes,如若转载,请注明出处:https://blog.fastrun.cn/2018/08/15/1-51/
评论列表(1条)
很受用!!