Commit 294256d2 by 李洋

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

parent ff1bf2cd
Showing with 136 additions and 129 deletions
......@@ -39,151 +39,158 @@ class MessageApiController extends Controller
$this->Export(11002, '请输入模板描述');
}
//消息模板模型
$db = DB::connection('message')->table('lie_msg_tpl');
$tpl_model = [];
$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, '编辑消息模板失败');
}
}
//添加事务 保证 数据库多条修改时 原子操作
DB::beginTransaction();
try {
//每一个渠道模板 的验证与写数据库
//选了站内信渠道 就要选站内信类型
if (strpos($data['channels'], '1') !== false) {
if (empty($data['msg_type'])) {
$this->Export(11003, '请选择站内信类型');
}
if (empty($data['inner_title'])) {
$this->Export(11004, '请填写站内信标题');
}
if (empty($data['inner_content'])) {
$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, '新增消息模板失败');
//消息模板模型
$tpl_model = [];
$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::connection('message')->table('lie_msg_tpl')->insertGetId($tpl_model);
if (!$insert_tpl_id) {
$this->Export(11020, '新增消息模板失败');
}
} 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);
$tpl_model['update_time'] = time();
$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) {
$this->Export(11009, '编辑消息模板失败');
$this->Export(11021, '编辑消息模板失败');
}
}
}
if (strpos($data['channels'], '2') !== false) {
if (empty($data['sms_content'])) {
$this->Export(11010, '请填写短信内容');
}
$chn_tpl_model_sms = [];
$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, '新增消息模板失败');
//每一个渠道模板 的验证与写数据库
//选了站内信渠道 就要选站内信类型
if (strpos($data['channels'], '1') !== false) {
if (empty($data['msg_type'])) {
$this->Export(11003, '请选择站内信类型');
}
} 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 (empty($data['inner_title'])) {
$this->Export(11004, '请填写站内信标题');
}
if (empty($data['inner_content'])) {
$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'], '3') !== false) {
if (empty($data['email_title'])) {
$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, '新增消息模板失败');
if (strpos($data['channels'], '2') !== false) {
if (empty($data['sms_content'])) {
$this->Export(11010, '请填写短信内容');
}
} 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_sms = [];
$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 {
$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'], '4') !== false) {
if (empty($data['wechat_notify_title'])) {
$this->Export(11017, '请填写微信模板编号');
if (strpos($data['channels'], '3') !== false) {
if (empty($data['email_title'])) {
$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_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 = [];
$chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
$chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
$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, '新增消息模板失败');
if (strpos($data['channels'], '4') !== false) {
if (empty($data['wechat_notify_title'])) {
$this->Export(11017, '请填写微信模板编号');
}
} 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, '编辑消息模板失败');
$chn_tpl_model_wechatnotify = [];
$chn_tpl_model_wechatnotify['channel_tpl_id'] = $data['wechat_notify_tpl_id'];
$chn_tpl_model_wechatnotify['channel_type'] = $data['wechat_notify_channel_type'];
$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['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']) ? '新增成功' : '修改成功');
}
......
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