package com.sun.springboot.controller;
import com.alibaba.fastjson.JSONObject;
import com.sun.springboot.bean.ApplyMessage;
import com.sun.springboot.bean.ApplyMessageNew;
import com.sun.springboot.bean.ApplyTableMessage;
import com.sun.springboot.bean.ApplyUser;
import com.sun.springboot.bean.ApplyVo;
import com.sun.springboot.bean.User;
import com.sun.springboot.service.ApplyMessageService;
import com.sun.springboot.service.ApplyUserService;
import com.sun.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author sunhongguang
* @create 2020-11-19-23:34
*/
@RestController
@RequestMapping(path = "/new/apply")
public class ApplyController_new {
@Autowired
private UserService userService;
@Autowired
private ApplyUserService applyUserService;
@Autowired
private ApplyMessageService applyMessageService;
@PostMapping(path = "/table")
public synchronized ResponseEntity applyTable(@RequestBody JSONObject jsonObject) {
// 获取申请人信息JSON对象
JSONObject applyPersonJson = jsonObject.getJSONObject("applyUser");
// 把申请人信息封装到ApplyUser对象中
ApplyUser applyUser = JSONObject.toJavaObject(applyPersonJson, ApplyUser.class);
// 获取申请的信息
JSONObject applyMessageJson = jsonObject.getJSONObject("applyMessageNew");
ApplyMessageNew applyMessageNew = JSONObject.toJavaObject(applyMessageJson, ApplyMessageNew.class);
// 获取申请人信息里面的批量申请人集合
List<String> batchApplyAccountList = applyMessageNew.getBatchApplyAccount();
List<ApplyTableMessage> applyTable = applyMessageNew.getApplyTable();
String test = applyTable.toString();
List<String> batchApplyAccount = applyMessageNew.getBatchApplyAccount();
String test1 = batchApplyAccount.toString();
//判断批量申请列表中是否有不合法用户
List<String> failList = judgeBatchApply(batchApplyAccountList);
if (!CollectionUtils.isEmpty(failList)) {
return ResponseEntity.ok(new HashMap<String, Object>() {{
put("code", "-1");
put("message", "申请失败!请检查批量申请的用户UM账号是否填写正确" + failList);
}});
}
// 判断当前用户是否为第一次申请
ApplyUser apply_user = this.applyUserService.findApplyUser(applyUser.getApplyUserAccount());
if (apply_user == null) { // 如果apply_user为null,则说明这是用户第一次申请
// 插入申请人信息到申请人信息表apply_user中
this.applyUserService.saveApplyUser(applyUser);
// 插入申请信息到申请表apply_message中
int userId = this.applyUserService.findApplyUserId(applyUser.getApplyUserAccount());
applyMessageNew.setApplyNum(1);
applyMessageNew.setApplyUserId(userId);
applyMessageNew.setTestTable(test);
applyMessageNew.setTestBatchApplyAccount(test1);
save(applyMessageNew);
} else {
// 如果此用户不是第一次申请,则先更新此用户(因为用户的用户名user_name,用户岗位user_job,用户部门user_department可能会变)
applyUser.setId(apply_user.getId());
this.applyUserService.updateApplyUser(applyUser);
// 此用户不是第一次申请,则先去用户申请表中查找此申请用户的id
int userId = this.applyUserService.findApplyUserId(apply_user.getApplyUserAccount());
// 到申请信息表中查找最大的申请批次号
int maxBatchApplyNum = this.applyUserService.findMaxBatchApplyNum_new(apply_user.getApplyUserAccount());
applyMessageNew.setApplyNum(maxBatchApplyNum+1);
applyMessageNew.setApplyUserId(apply_user.getId());
applyMessageNew.setTestTable(test);
applyMessageNew.setTestBatchApplyAccount(test1);
save(applyMessageNew);
}
return ResponseEntity.ok(new HashMap<String, Object>() {{
put("code", 0);
put("message", "申请成功,待申批。");
}});
}
private void save(ApplyMessageNew applyMessageNew) {
this.applyMessageService.save(applyMessageNew);
}
/**
* 判断批量申请列表中是否有不合法用户
*
* @param batchApplyAccountList 批量申请的用户列表
* @return 批量申请列表中是否有不合法的UM账号
*/
private List<String> judgeBatchApply(List<String> batchApplyAccountList) {
List<String> failList = new ArrayList<>();
if (!CollectionUtils.isEmpty(batchApplyAccountList)) {
// 遍历批量申请人,到用户表中查找
for (String account : batchApplyAccountList) {
User user = this.userService.findUserByAccount(account);
if (user == null) {
failList.add(account);
}
}
} else { // 如果没有批量申请人,则直接返回true
return failList;
}
return failList;
}
}
package com.sun.springboot.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.util.Date;
import java.util.List;
/**
* @author sunhongguang
* @create 2020-11-19-23:30
*/
public class ApplyMessageNew {
private Integer id;
private Integer applyUserId;
private String eventName;
private List<String> batchApplyAccount;
private List<ApplyTableMessage> applyTable;
private String applyReason;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
private Date applyDate;
private Integer applyNum;
private String testTable;
private String testBatchApplyAccount;
public String getTestBatchApplyAccount() {
return testBatchApplyAccount;
}
public void setTestBatchApplyAccount(String testBatchApplyAccount) {
this.testBatchApplyAccount = testBatchApplyAccount;
}
public String getTestTable() {
return testTable;
}
public void setTestTable(String testTable) {
this.testTable = testTable;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getApplyUserId() {
return applyUserId;
}
public void setApplyUserId(Integer applyUserId) {
this.applyUserId = applyUserId;
}
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public List<String> getBatchApplyAccount() {
return batchApplyAccount;
}
public void setBatchApplyAccount(List<String> batchApplyAccount) {
this.batchApplyAccount = batchApplyAccount;
}
public List<ApplyTableMessage> getApplyTable() {
return applyTable;
}
public void setApplyTable(List<ApplyTableMessage> applyTable) {
this.applyTable = applyTable;
}
public String getApplyReason() {
return applyReason;
}
public void setApplyReason(String applyReason) {
this.applyReason = applyReason;
}
public Date getApplyDate() {
return applyDate;
}
public void setApplyDate(Date applyDate) {
this.applyDate = applyDate;
}
public Integer getApplyNum() {
return applyNum;
}
public void setApplyNum(Integer applyNum) {
this.applyNum = applyNum;
}
@Override
public String toString() {
return "ApplyMessageNew{" +
"id=" + id +
", applyUserId=" + applyUserId +
", eventName='" + eventName + '\'' +
", batchApplyAccount=" + batchApplyAccount +
", applyTable=" + applyTable +
", applyReason='" + applyReason + '\'' +
", applyDate=" + applyDate +
", applyNum=" + applyNum +
", testTable='" + testTable + '\'' +
", testBatchApplyAccount='" + testBatchApplyAccount + '\'' +
'}';
}
}
package com.sun.springboot.bean;
/**
* @author sunhongguang
* @create 2020-11-20-0:05
*/
public class ApplyNew {
private ApplyUser applyUser;
private ApplyMessageNew applyMessageNew;
public ApplyUser getApplyUser() {
return applyUser;
}
public void setApplyUser(ApplyUser applyUser) {
this.applyUser = applyUser;
}
public ApplyMessageNew getApplyMessageNew() {
return applyMessageNew;
}
public void setApplyMessageNew(ApplyMessageNew applyMessageNew) {
this.applyMessageNew = applyMessageNew;
}
@Override
public String toString() {
return "ApplyNew{" +
"applyUser=" + applyUser +
", applyMessageNew=" + applyMessageNew +
'}';
}
}
package com.sun.springboot.test;
import com.alibaba.fastjson.JSON;
import com.sun.springboot.bean.Apply;
import com.sun.springboot.bean.ApplyMessage;
import com.sun.springboot.bean.ApplyMessageNew;
import com.sun.springboot.bean.ApplyNew;
import com.sun.springboot.bean.ApplyTableMessage;
import com.sun.springboot.bean.ApplyUser;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author sunhongguang
* @create 2020-11-14-10:24
*/
public class MainTest {
public static void main(String[] args) {
// 申请人对象构造
ApplyUser applyUser = new ApplyUser();
applyUser.setApplyUserName("孙鸿广"); // 申请人姓名
applyUser.setApplyUserAccount("SUNHONGGUANG454"); // 申请人UM账户
applyUser.setApplyJob("Java"); // 申请人职位
applyUser.setApplyDepartment("GBD"); // 申请人部门
// 批量申请人集合
List<String> batchApplyAccountList = new ArrayList<>();
batchApplyAccountList.add("shg1");
batchApplyAccountList.add("shg2");
batchApplyAccountList.add("shg3");
ApplyMessageNew applyMessageNew = new ApplyMessageNew();
applyMessageNew.setEventName("测试...");
applyMessageNew.setBatchApplyAccount(batchApplyAccountList);
// 申请的表信息
List<ApplyTableMessage> applyTableMessageList = new ArrayList<>();
ApplyTableMessage applyTableMessage1 = new ApplyTableMessage(1,"database_1","table_1","表注释1");
ApplyTableMessage applyTableMessage2 = new ApplyTableMessage(2,"database_2","table_2","表注释2");
ApplyTableMessage applyTableMessage3 = new ApplyTableMessage(3,"database_3","table_3","表注释3");
applyTableMessageList.add(applyTableMessage1);
applyTableMessageList.add(applyTableMessage2);
applyTableMessageList.add(applyTableMessage3);
applyMessageNew.setApplyTable(applyTableMessageList);
applyMessageNew.setApplyReason("test...");
applyMessageNew.setApplyDate(new Date());
ApplyNew applyNew = new ApplyNew();
applyNew.setApplyUser(applyUser);
applyNew.setApplyMessageNew(applyMessageNew);
String jsonString = JSON.toJSONString(applyNew,true);
System.out.println(jsonString);
}
}
{
"applyMessageNew":{
"applyDate":1605802027384,
"applyReason":"test...",
"applyTable":[
{
"databaseName":"database_1",
"id":1,
"tableComment":"表注释1",
"tableName":"table_1"
},
{
"databaseName":"database_2",
"id":2,
"tableComment":"表注释2",
"tableName":"table_2"
},
{
"databaseName":"database_3",
"id":3,
"tableComment":"表注释3",
"tableName":"table_3"
}
],
"batchApplyAccount":[
"LISI001",
"ZHANGSAN002"
],
"eventName":"测试..."
},
"applyUser":{
"applyDepartment":"DGD",
"applyJob":"C++",
"applyUserAccount":"ZHANGSAN002",
"applyUserName":"张三2"
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sun.springboot.mapper.ApplyMessageMapper">
<!--保存申请信息到申请表中-->
<insert id="saveApplyMessage" parameterType="com.sun.springboot.bean.ApplyVo">
INSERT INTO `pingan`.`apply_message`
(
`apply_user_id`,
`apply_by`,
`event_name`,
`apply_date`,
`database_name`,
`table_name`,
`table_comment`,
`apply_reason`,
`apply_batch_num`
)
VALUES
(
#{applyVo.applyUserId},
#{applyVo.applyBy},
#{applyVo.eventName},
#{applyVo.applyDate},
#{applyVo.databaseName},
#{applyVo.tableName},
#{applyVo.tableComment},
#{applyVo.applyReason},
#{applyVo.applyBatchNum}
);
</insert>
<insert id="save" parameterType="com.sun.springboot.bean.ApplyMessageNew">
INSERT INTO `pingan`.`apply_message_new`
(
`apply_user_id`,
`event_name`,
`batch_apply_account`,
`apply_table`,
`apply_reason`,
`apply_date`,
`apply_batch_num`
)
VALUES
(
#{applyMessageNew.applyUserId},
#{applyMessageNew.eventName},
#{applyMessageNew.testBatchApplyAccount},
#{applyMessageNew.testTable},
#{applyMessageNew.applyReason},
#{applyMessageNew.applyDate},
#{applyMessageNew.applyNum}
)
</insert>
</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sun.springboot.mapper.ApplyUserMapper">
<resultMap id="applyUserMap" type="com.sun.springboot.bean.ApplyUser">
<id property="id" column="id"/>
<result property="applyUserName" column="apply_user_name"/>
<result property="applyUserAccount" column="apply_user_um_account"/>
<result property="applyJob" column="apply_user_job"/>
<result property="applyDepartment" column="apply_user_department"/>
</resultMap>
<!--查询申请人信息-->
<select id="findApplyUser" resultMap="applyUserMap">
select * from apply_user where apply_user_um_account =#{applyUserAccount}
</select>
<!--插入申请人信息到申请表中-->
<insert id="saveApplyUser" parameterType="com.sun.springboot.bean.ApplyUser">
INSERT INTO `pingan`.`apply_user`
(
`apply_user_name`,
`apply_user_um_account`,
`apply_user_job`,
`apply_user_department`
)
VALUES
(
#{applyUser.applyUserName},
#{applyUser.applyUserAccount},
#{applyUser.applyJob},
#{applyUser.applyDepartment}
);
</insert>
<!--查找申请人的ID-->
<select id="findApplyUserId" resultType="java.lang.Integer">
select id from apply_user where apply_user_um_account = #{applyUserAccount}
</select>
<!--查找申请人最大的申请批次号-->
<select id="findMaxBatchApplyNum" resultType="java.lang.Integer">
SELECT IFNULL(MAX(apply_batch_num),0)
FROM apply_message
WHERE apply_user_id =
(SELECT id FROM apply_user WHERE apply_user_um_account = #{applyUserAccount});
</select>
<!--查找申请人最大的申请批次号-->
<select id="findMaxBatchApplyNum_new" resultType="java.lang.Integer">
SELECT IFNULL(MAX(apply_batch_num),0)
FROM apply_message_new
WHERE apply_user_id =
(SELECT id FROM apply_user WHERE apply_user_um_account = #{applyUserAccount});
</select>
<!--更新申请的用户信息-->
<update id="updateApplyUser" parameterType="com.sun.springboot.bean.ApplyUser">
UPDATE `pingan`.`apply_user`
SET
`apply_user_name` = #{apply_user.applyUserName} ,
`apply_user_job` = #{apply_user.applyJob} ,
`apply_user_department` = #{apply_user.applyDepartment}
WHERE
`id` = #{apply_user.id};
</update>
</mapper>