package com.highdatas.mdm.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.highdatas.mdm.entity.Flows;
import com.highdatas.mdm.entity.Maintain;
import com.highdatas.mdm.entity.MaintainDetail;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.mapper.MaintainDetailMapper;
import com.highdatas.mdm.mapper.TableInfoMapper;
import com.highdatas.mdm.pojo.ActivitiStatus;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.Operate;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.*;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.ContentBuilder;
import com.highdatas.mdm.util.DbUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;
/**
*
* 前端控制器
*
*
* @author kimi
* @since 2019-12-16
*/
@RestController
@RequestMapping("/maintain")
public class MaintainController {
@Autowired
IMaintainService maintainService;
@Autowired
IMaintainDetailService maintainDetailService;
@Autowired
TableInfoMapper tableInfoMapper;
@Autowired
IFlowsService flowsService;
@Autowired
IMaintainFieldService maintainFieldService;
@Autowired
IMasterAuthorService masterAuthorService;
@Autowired
MaintainDetailMapper maintainDetailMapper;
@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
public Result get(@PathVariable String id) {
Maintain maintain = maintainService.selectById(id);
if (maintain == null) {
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
}
return Result.success(maintain);
}
@RequestMapping(value = "/getPreInfo/{id}", method = RequestMethod.GET)
public Result getPreInfo(@PathVariable String id, @RequestParam String maintainId) {
Maintain maintain = maintainService.selectById(maintainId);
if (maintain == null) {
return Result.error(CodeMsg.SELECT_ERROR_NOTFOUND);
}
Map preInfo = maintainService.getPreInfo(maintain, id);
return Result.success(preInfo);
}
@RequestMapping(value = "{tableName}/unSubmit/{pageNo}", method = RequestMethod.GET)
public Result unSubmit(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
String pageSize = request.getParameter("pageSize");
String whereSegment = request.getParameter("whereSegment");
TUser user = DbUtils.getUser(request);
if (StringUtils.isEmpty(whereSegment)) {
whereSegment = Constant.WHERE_DEFAULT;
}else {
whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
}
return maintainService.getUnSubmitData(user, tableName, pageNo, pageSize, whereSegment);
}
@RequestMapping(value = "{tableName}/unSubmitCnt", method = RequestMethod.GET)
public Result unSubmitCnt(@PathVariable String tableName,HttpServletRequest request) throws UnsupportedEncodingException {
String whereSegment = request.getParameter("whereSegment");
TUser user = DbUtils.getUser(request);
if (StringUtils.isEmpty(whereSegment)) {
whereSegment = Constant.WHERE_DEFAULT;
}else {
whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
}
Long unSubmitDataCnt = maintainService.getUnSubmitDataCnt(user, tableName, whereSegment);
return Result.success(unSubmitDataCnt);
}
@RequestMapping(value = "{tableName}/unflowCnt", method = RequestMethod.GET)
public Result unflow(@PathVariable String tableName, HttpServletRequest request) throws UnsupportedEncodingException {
String whereSegment = request.getParameter("whereSegment");
TUser user = DbUtils.getUser(request);
if (StringUtils.isEmpty(whereSegment)) {
whereSegment = Constant.WHERE_DEFAULT;
}else {
whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
}
Long invalidVerionDataCnt = maintainService.getInvalidVerionDataCnt(user, tableName, whereSegment);
return Result.success(invalidVerionDataCnt);
}
@RequestMapping(value = "{tableName}/unflow/{pageNo}", method = RequestMethod.GET)
public Result unflow(@PathVariable String tableName, @PathVariable Integer pageNo, HttpServletRequest request) throws UnsupportedEncodingException {
String pageSize = request.getParameter("pageSize");
String whereSegment = request.getParameter("whereSegment");
TUser user = DbUtils.getUser(request);
if (StringUtils.isEmpty(whereSegment)) {
whereSegment = Constant.WHERE_DEFAULT;
}else {
whereSegment = URLDecoder.decode(whereSegment, "UTF-8");
}
if (StringUtils.isEmpty(pageSize)) {
return maintainService.getInvalidVerionData(user, tableName, whereSegment, pageNo, null);
} else {
return maintainService.getInvalidVerionData(user, tableName, whereSegment, pageNo, Integer.valueOf(pageSize));
}
}
@RequestMapping(value = "/version/{tableName}", method = RequestMethod.GET)
public Result getHistory(@PathVariable String tableName, HttpServletRequest request) {
TUser user = DbUtils.getUser(request);
String userId = user.getUserId();
Maintain maxVersion = maintainService.getMaxVersion(tableName);
if (maxVersion == null) {
return Result.error(new CodeMsg(100001,"无当前版本"));
}
Maintain nowVersion = maintainService.getNowVersion(tableName);
List maintainList = maintainService.selectList(new EntityWrapper().eq("table_name", tableName).orderBy("order_no",false));
List result = new ArrayList<>();
int count = 0;
for (Maintain maintain : maintainList) {
String flowId = maintain.getFlowId();
if (StringUtils.isEmpty(flowId)) {
continue;
}
boolean author = masterAuthorService.checkMaintainAuthor(user, maintain.getId());
if (!author) {
continue;
}
Flows flows = flowsService.selectById(maintain.getFlowId());
if (flows.getStatus().equals(ActivitiStatus.close)) {
continue;
}
if (flows.getStatus().equals(ActivitiStatus.working) || flows.getStatus().equals(ActivitiStatus.refuse)) {
boolean nextAudit = flowsService.isNextAudit(flows, userId);
if (nextAudit) {
count++;
}
// count++;
continue;
}else if(flows.getStatus().equals(ActivitiStatus.open)){
maintain.setMaintainType(1);
}
if (nowVersion != null && maintain.getId().equalsIgnoreCase(nowVersion.getId())) {
maintain.setMaintainType(0);
}
result.add(maintain);
}
//
int cnt = maintainFieldService.getUnFlowCount(tableName, userId);
count += cnt;
if (count > 0) {
Maintain maintain = new Maintain();
maintain.setMaintainType(-1);
maintain.setVersion("待审核");
maintain.setTableName(tableName);
maintain.setRecordCount(count);
result.add(maintain);
}
Collections.sort(result, new Comparator() {
@Override
public int compare(Maintain o1, Maintain o2) {
return o1.getMaintainType().compareTo(o2.getMaintainType());
}
});
return Result.success(result);
}
@RequestMapping(value = "/detail/{id}/{pageNo}", method = RequestMethod.GET)
public Result detail(@PathVariable String id, @PathVariable int pageNo) {
Maintain maintain = maintainService.selectById(id);
String tableName = maintain.getTableName();
String tableTempName = tableName + Constant.RECORD;
List maintainDetailList = maintainDetailService.selectList(new EntityWrapper().eq("parent_id", id));
return Result.success(maintain);
}
@RequestMapping(value = "/all/{pageno}", method = RequestMethod.GET)
public Result all(@PathVariable int pageno, HttpServletRequest request) {
String tableName = request.getParameter("tableName");
String s = tableInfoMapper.selectTableByName(tableName);
Page result;
String pageSize = request.getParameter("pageSize");
Page maintainPage;
if (StringUtils.isEmpty(pageSize)) {
maintainPage = new Page(pageno, 20);
} else {
maintainPage = new Page(pageno, Integer.valueOf(pageSize));
}
if (StringUtils.isEmpty(tableName)) {
result = maintainService.selectPage(maintainPage);
} else {
Wrapper tableNameWrapper = new EntityWrapper().eq("table_name", tableName);
tableNameWrapper.orderBy("order_no desc");
result = maintainService.selectPage(maintainPage, tableNameWrapper);
}
List records = result.getRecords();
List resultObj = new ArrayList<>();
for (Maintain record : records) {
String flowId = record.getFlowId();
if (StringUtils.isEmpty(flowId)) {
continue;
}
Flows flows = flowsService.selectById(flowId);
if (flows == null || !flows.getStatus().equals(ActivitiStatus.open)) {
continue;
}
String chargeId = record.getChargeId();
TUser user = DbUtils.getUserById(chargeId);
if (user == null) {
record.setChargeId("用户已删除");
}else {
record.setChargeId(user.getUserName());
}
resultObj.add(record);
}
result.setRecords(resultObj);
return Result.success(result);
}
@RequestMapping(value = "/compareList/{pageNo}", method = RequestMethod.GET)
public Result compare(@PathVariable Integer pageNo, @RequestParam String tableName, HttpServletRequest request) {
String pageSizeStr = request.getParameter("pageSize");
int pageSize = 15;
if (!StringUtils.isEmpty(pageSizeStr)) {
pageSize = Integer.valueOf(pageSizeStr);
}
List maintainList = maintainService.selectList(new EntityWrapper().eq("table_name", tableName).isNotNull("flow_id").orderBy("order_no desc"));
com.highdatas.mdm.pojo.Page page = new com.highdatas.mdm.pojo.Page(maintainList.size());
page.setPageNo(pageNo);
page.setPageSize(pageSize);
List collect = maintainList.stream().filter(maintain -> ActivitiStatus.open.equals(flowsService.getStatusByBusinessId(maintain.getId())))
.skip(page.getBeginRecordNo_1()).limit(pageSize).collect(Collectors.toList());
List> result = new ArrayList<>();
for (int i = 0; i < collect.size(); i++) {
Maintain maintain = collect.get(i);
String version = maintain.getVersion();
Date createTime = maintain.getCreateTime();
String chargeId = maintain.getChargeId();
TUser user = DbUtils.getUserById(chargeId);
HashMap oneResult = new HashMap<>();
oneResult.put("version", version);
oneResult.put("time", createTime);
oneResult.put("userName", chargeId);
oneResult.put("id", maintain.getId());
if (user != null) {
oneResult.put("userName", user.getUserName());
}
if (i < collect.size() - 1) {
Maintain preMaintain = collect.get(i + 1);
List