Commit 294256d2 by 李洋

添加事务 保证 数据库多条修改时 原子操作

parent ff1bf2cd
Showing with 136 additions and 129 deletions
...@@ -39,151 +39,158 @@ class MessageApiController extends Controller ...@@ -39,151 +39,158 @@ class MessageApiController extends Controller
$this->Export(11002, '请输入模板描述'); $this->Export(11002, '请输入模板描述');
} }
//消息模板模型 //添加事务 保证 数据库多条修改时 原子操作
$db = DB::connection('message')->table('lie_msg_tpl'); DB::beginTransaction();
$tpl_model = []; try {
$tpl_model['tpl_id'] = $data['tpl_id'];
$tpl_model['channels'] = $data['channels'];
$tpl_model['description'] = $data['description'];
$tpl_model['source_type'] = $data['source_type'];
$tpl_model['creater'] = $data['creater'];
// $tpl_model['obj_user'] = $data['obj_user'];
$tpl_model['msg_type'] = $data['msg_type'];
//添加事务
if (empty($tpl_model['tpl_id'])) {
$tpl_model['create_time'] = time();
$tpl_model['update_time'] = time();
$insert_tpl_id = $db->insertGetId($tpl_model);
if (!$insert_tpl_id) {
$this->Export(11020, '新增消息模板失败');
}
} else {
$tpl_model['update_time'] = time();
$result = $db->where('tpl_id', $tpl_model['tpl_id'])->update($tpl_model);
$insert_tpl_id = $tpl_model['tpl_id'];
if (!$result) {
$this->Export(11021, '编辑消息模板失败');
}
}
//每一个渠道模板 的验证与写数据库 //消息模板模型
//选了站内信渠道 就要选站内信类型 $tpl_model = [];
if (strpos($data['channels'], '1') !== false) { $tpl_model['tpl_id'] = $data['tpl_id'];
if (empty($data['msg_type'])) { $tpl_model['channels'] = $data['channels'];
$this->Export(11003, '请选择站内信类型'); $tpl_model['description'] = $data['description'];
} $tpl_model['source_type'] = $data['source_type'];
if (empty($data['inner_title'])) { $tpl_model['creater'] = $data['creater'];
$this->Export(11004, '请填写站内信标题'); // $tpl_model['obj_user'] = $data['obj_user'];
} $tpl_model['msg_type'] = $data['msg_type'];
if (empty($data['inner_content'])) { //添加事务
$this->Export(11005, '请填写站内信内容'); if (empty($tpl_model['tpl_id'])) {
} $tpl_model['create_time'] = time();
if (empty($data['op_type'])) { $tpl_model['update_time'] = time();
$this->Export(11006, '请选择点击消息操作'); $insert_tpl_id = DB::connection('message')->table('lie_msg_tpl')->insertGetId($tpl_model);
} if (!$insert_tpl_id) {
if (empty($data['url']) && $data['op_type'] == 2) { $this->Export(11020, '新增消息模板失败');
$this->Export(11007, '请填写要打开的url');
}
$chn_tpl_model_inner = [];
$chn_tpl_model_inner['channel_tpl_id'] = $data['inner_channel_tpl_id'];
$chn_tpl_model_inner['channel_type'] = $data['inner_channel_type'];
$chn_tpl_model_inner['title'] = $data['inner_title'];
$chn_tpl_model_inner['content'] = $data['inner_content'];
$chn_tpl_model_inner['op_type'] = $data['op_type'];
$chn_tpl_model_inner['tpl_id'] = $insert_tpl_id;
if ($data['op_type'] == 2) {
$chn_tpl_model_inner['url'] = $data['url'];
}
if (empty($chn_tpl_model_inner['channel_tpl_id'])) {
$chn_tpl_model_inner['create_time'] = time();
$chn_tpl_model_inner['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_inner);
if (!$result) {
$this->Export(11008, '新增消息模板失败');
} }
} else { } else {
$chn_tpl_model_inner['update_time'] = time(); $tpl_model['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_inner['channel_tpl_id'])->update($chn_tpl_model_inner); $result = DB::connection('message')->table('lie_msg_tpl')->where('tpl_id', $tpl_model['tpl_id'])->update($tpl_model);
$insert_tpl_id = $tpl_model['tpl_id'];
if (!$result) { if (!$result) {
$this->Export(11009, '编辑消息模板失败'); $this->Export(11021, '编辑消息模板失败');
} }
} }
}
if (strpos($data['channels'], '2') !== false) { //每一个渠道模板 的验证与写数据库
if (empty($data['sms_content'])) { //选了站内信渠道 就要选站内信类型
$this->Export(11010, '请填写短信内容'); if (strpos($data['channels'], '1') !== false) {
} if (empty($data['msg_type'])) {
$chn_tpl_model_sms = []; $this->Export(11003, '请选择站内信类型');
$chn_tpl_model_sms['channel_tpl_id'] = $data['sms_channel_tpl_id'];
$chn_tpl_model_sms['channel_type'] = $data['sms_channel_type'];
$chn_tpl_model_sms['content'] = $data['sms_content'];
$chn_tpl_model_sms['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_sms['channel_tpl_id'])) {
$chn_tpl_model_sms['create_time'] = time();
$chn_tpl_model_sms['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_sms);
if (!$result) {
$this->Export(11011, '新增消息模板失败');
} }
} else { if (empty($data['inner_title'])) {
$chn_tpl_model_sms['update_time'] = time(); $this->Export(11004, '请填写站内信标题');
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_sms['channel_tpl_id'])->update($chn_tpl_model_sms); }
if (!$result) { if (empty($data['inner_content'])) {
$this->Export(11012, '编辑消息模板失败'); $this->Export(11005, '请填写站内信内容');
}
if (empty($data['op_type'])) {
$this->Export(11006, '请选择点击消息操作');
}
if (empty($data['url']) && $data['op_type'] == 2) {
$this->Export(11007, '请填写要打开的url');
}
$chn_tpl_model_inner = [];
$chn_tpl_model_inner['channel_tpl_id'] = $data['inner_channel_tpl_id'];
$chn_tpl_model_inner['channel_type'] = $data['inner_channel_type'];
$chn_tpl_model_inner['title'] = $data['inner_title'];
$chn_tpl_model_inner['content'] = $data['inner_content'];
$chn_tpl_model_inner['op_type'] = $data['op_type'];
$chn_tpl_model_inner['tpl_id'] = $insert_tpl_id;
if ($data['op_type'] == 2) {
$chn_tpl_model_inner['url'] = $data['url'];
}
if (empty($chn_tpl_model_inner['channel_tpl_id'])) {
$chn_tpl_model_inner['create_time'] = time();
$chn_tpl_model_inner['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_inner);
if (!$result) {
$this->Export(11008, '新增消息模板失败');
}
} else {
$chn_tpl_model_inner['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_inner['channel_tpl_id'])->update($chn_tpl_model_inner);
if (!$result) {
$this->Export(11009, '编辑消息模板失败');
}
} }
} }
} if (strpos($data['channels'], '2') !== false) {
if (strpos($data['channels'], '3') !== false) { if (empty($data['sms_content'])) {
if (empty($data['email_title'])) { $this->Export(11010, '请填写短信内容');
$this->Export(11013, '请填写邮件标题');
}
if (empty($data['email_content'])) {
$this->Export(11014, '请填写邮件内容');
}
$chn_tpl_model_email = [];
$chn_tpl_model_email['channel_tpl_id'] = $data['email_channel_tpl_id'];
$chn_tpl_model_email['channel_type'] = $data['email_channel_type'];
$chn_tpl_model_email['title'] = $data['email_title'];
$chn_tpl_model_email['content'] = $data['email_content'];
$chn_tpl_model_email['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_email['channel_tpl_id'])) {
$chn_tpl_model_email['create_time'] = time();
$chn_tpl_model_email['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_email);
if (!$result) {
$this->Export(11015, '新增消息模板失败');
} }
} else { $chn_tpl_model_sms = [];
$chn_tpl_model_email['update_time'] = time(); $chn_tpl_model_sms['channel_tpl_id'] = $data['sms_channel_tpl_id'];
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_email['channel_tpl_id'])->update($chn_tpl_model_email); $chn_tpl_model_sms['channel_type'] = $data['sms_channel_type'];
if (!$result) { $chn_tpl_model_sms['content'] = $data['sms_content'];
$this->Export(11016, '编辑消息模板失败',$result); $chn_tpl_model_sms['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_sms['channel_tpl_id'])) {
$chn_tpl_model_sms['create_time'] = time();
$chn_tpl_model_sms['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_sms);
if (!$result) {
$this->Export(11011, '新增消息模板失败');
}
} else {
$chn_tpl_model_sms['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_sms['channel_tpl_id'])->update($chn_tpl_model_sms);
if (!$result) {
$this->Export(11012, '编辑消息模板失败');
}
} }
} }
} if (strpos($data['channels'], '3') !== false) {
if (strpos($data['channels'], '4') !== false) { if (empty($data['email_title'])) {
if (empty($data['wechat_notify_title'])) { $this->Export(11013, '请填写邮件标题');
$this->Export(11017, '请填写微信模板编号'); }
if (empty($data['email_content'])) {
$this->Export(11014, '请填写邮件内容');
}
$chn_tpl_model_email = [];
$chn_tpl_model_email['channel_tpl_id'] = $data['email_channel_tpl_id'];
$chn_tpl_model_email['channel_type'] = $data['email_channel_type'];
$chn_tpl_model_email['title'] = $data['email_title'];
$chn_tpl_model_email['content'] = $data['email_content'];
$chn_tpl_model_email['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_email['channel_tpl_id'])) {
$chn_tpl_model_email['create_time'] = time();
$chn_tpl_model_email['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_email);
if (!$result) {
$this->Export(11015, '新增消息模板失败');
}
} else {
$chn_tpl_model_email['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_email['channel_tpl_id'])->update($chn_tpl_model_email);
if (!$result) {
$this->Export(11016, '编辑消息模板失败', $result);
}
}
} }
$chn_tpl_model_wechatnotify = []; if (strpos($data['channels'], '4') !== false) {
$chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id']; if (empty($data['wechat_notify_title'])) {
$chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type']; $this->Export(11017, '请填写微信模板编号');
$chn_tpl_model_wechatnotify['title'] = $data['wechat_notify_title'];
$chn_tpl_model_wechatnotify['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_wechatnotify['channel_tpl_id'])) {
$chn_tpl_model_wechatnotify['create_time'] = time();
$chn_tpl_model_wechatnotify['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_wechatnotify);
if (!$result) {
$this->Export(11018, '新增消息模板失败');
} }
} else { $chn_tpl_model_wechatnotify = [];
$chn_tpl_model_wechatnotify['update_time'] = time(); $chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_wechatnotify['channel_tpl_id'])->update($chn_tpl_model_wechatnotify); $chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
if (!$result) { $chn_tpl_model_wechatnotify['title'] = $data['wechat_notify_title'];
$this->Export(11019, '编辑消息模板失败'); $chn_tpl_model_wechatnotify['tpl_id'] = $insert_tpl_id;
if (empty($chn_tpl_model_wechatnotify['channel_tpl_id'])) {
$chn_tpl_model_wechatnotify['create_time'] = time();
$chn_tpl_model_wechatnotify['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->insert($chn_tpl_model_wechatnotify);
if (!$result) {
$this->Export(11018, '新增消息模板失败');
}
} else {
$chn_tpl_model_wechatnotify['update_time'] = time();
$result = DB::connection('message')->table('lie_msg_channel_tpl')->where('channel_tpl_id', $chn_tpl_model_wechatnotify['channel_tpl_id'])->update($chn_tpl_model_wechatnotify);
if (!$result) {
$this->Export(11019, '编辑消息模板失败');
}
} }
} }
DB::commit();
}catch (\Exception $e){
DB::rollBack();
} }
$this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功'); $this->Export(0, empty($tpl_model['tpl_id']) ? '新增成功' : '修改成功');
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment