package com.highdatas.mdm.controller;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.highdatas.mdm.entity.Character;
import com.highdatas.mdm.entity.*;
import com.highdatas.mdm.mapper.SysFieldMapper;
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.AntianaphylaxisClient;
import com.highdatas.mdm.util.Constant;
import com.highdatas.mdm.util.DbUtils;
import com.highdatas.mdm.util.WorkflowUtils;
import lombok.extern.slf4j.Slf4j;
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 javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
/**
*
* 前端控制器
*
*
* @author kimi
* @since 2019-12-16
*/
@Slf4j
@RestController
@RequestMapping("/field")
public class SysFieldController {
@Autowired
ISysFieldService fieldService;
@Autowired
ISysMenuService menuService;
@Autowired
IMaintainService maintainService;
@Autowired
IMenuMappingService menuMappingService;
@Autowired
IMaintainFieldService maintainFieldService;
@Autowired
SysFieldMapper fieldMapper;
@Autowired
IFlowsService flowsService;
@Autowired
IMasterAuthorService masterAuthorService;
@Autowired
AntianaphylaxisClient antianaphylaxisClient;
@RequestMapping(value = "/{tableName}", method = RequestMethod.GET)
public Result getFields(@PathVariable String tableName, HttpServletRequest request){
Character character = DbUtils.getCharacter(request);
String maintainId = request.getParameter("maintainId");
if (StringUtils.isEmpty(maintainId)) {
List fieldList = fieldService.getFieldByTable(tableName);
String isDataIO = request.getParameter("isDataIO");
if (StringUtils.isEmpty(isDataIO) && !Boolean.valueOf(isDataIO)) {
return Result.success(fieldList);
}
antianaphylaxisClient.getHelpfulFieldBySysField(fieldList, tableName);
return Result.success(fieldList);
}else {
List field = masterAuthorService.getField(character, maintainId);
//List fieldList = fieldService.getFieldByMaintain(maintainId);
return Result.success(field);
}
}
@RequestMapping(value = "total/{tableName}/{pageNo}", method = RequestMethod.GET)
public Result getTotalFields(@PathVariable String tableName,@PathVariable Integer pageNo, HttpServletRequest request){
String totalStr = request.getParameter("isTotal");
String pageSize = request.getParameter("pageSize");
if (StringUtils.isEmpty(totalStr) || !Boolean.valueOf(totalStr)) {
// 走权限
String maintainId = request.getParameter("maintainId");
if (StringUtils.isEmpty(maintainId)){
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
TUser user = DbUtils.getUser(request);
List field = masterAuthorService.getField(user, maintainId);
Integer size;
if(StringUtils.isEmpty(pageSize)){
size = 15;
}else {
size = Integer.valueOf(pageSize);
}
return fieldService.getPagedDataByList(field, pageNo, size);
}
if(StringUtils.isEmpty(pageSize)){
return fieldService.getTotalFields(tableName, pageNo, 15);
}else {
return fieldService.getTotalFields(tableName, pageNo, Integer.valueOf(pageSize));
}
}
@RequestMapping(value = "fix/{tableName}", method = RequestMethod.GET)
public Result fix(@PathVariable String tableName, HttpServletRequest request){
Result totalFields = fieldService.getTotalFields(tableName);
JSONObject data = (JSONObject) totalFields.getData();
data.fluentPut("aduit", true);
return totalFields;
}
@RequestMapping(value = "/menu/{id}", method = RequestMethod.GET)
public Result getFieldByMenu(@PathVariable String id){
MenuMapping menuMapping = menuMappingService.selectOne(new EntityWrapper().eq("menu_id", id));
if (menuMapping == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
String tableName = menuMapping.getTableName();
if (StringUtils.isEmpty(tableName)) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
List fieldList = fieldService.getFieldByTable(tableName);
return Result.success(fieldList);
}
@RequestMapping(value = "/update", method = RequestMethod.GET)
public Result update(@RequestParam String json) {
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
SysField sysField = objectMapper.readValue(json, SysField.class);
sysField.setUpdateTime(new Date());
sysField.updateById();
return Result.success(null);
}catch (Exception e) {
e.printStackTrace();
return Result.error(CodeMsg.UPDATE_ERROR);
}
}
@RequestMapping(value = "/updateCommon/{id}", method = RequestMethod.GET)
public Result addOrUpdate(@PathVariable String id, HttpServletRequest request) {
//更新基础参数不需要关联版本
SysField sysField = fieldService.selectById(id);
if (sysField == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
String width = request.getParameter("width");
String align = request.getParameter("align");
String orderNoStr = request.getParameter("orderNo");
String desp = request.getParameter("desp");
if (!StringUtils.isEmpty(width)) {
sysField.setWidth(Integer.valueOf(width));
}
if (!StringUtils.isEmpty(desp)) {
sysField.setDesp(desp);
}
if (!StringUtils.isEmpty(align)) {
sysField.setAlign(align);
}
//orderno
if (!StringUtils.isEmpty(orderNoStr)) {
Integer preOrderNo = sysField.getOrderNo();
Integer orderNo = Integer.valueOf(orderNoStr);
String maintainId = sysField.getMaintainFieldId();
String maintainParams;
if (StringUtils.isEmpty(maintainId)) {
//初始状态
String tableName = sysField.getTableName();
maintainParams = "maintain_id is null and table_name = " + DbUtils.quotedStr(tableName);
}else {
maintainParams = "maintain_id = " + DbUtils.quotedStr(maintainId);
}
Integer updateSize;
if (preOrderNo > orderNo) {
//往前调
updateSize = fieldMapper.updateOrderNoFront(maintainParams,orderNo, preOrderNo);
}else {
updateSize = fieldMapper.updateOrderNoBack(maintainParams,orderNo, preOrderNo);
}
if (updateSize == null) {
return Result.error(CodeMsg.UPDATE_ERROR);
}
}
boolean update = sysField.updateById();
if (update) {
return Result.success(sysField);
}else {
return Result.error(CodeMsg.UPDATE_ERROR);
}
}
@RequestMapping(value = "/loadFields", method = RequestMethod.POST)
public Result loadFields(@RequestBody String fieldListStr, HttpServletRequest request) {
log.info(fieldListStr);
List fieldList = JSONObject.parseArray(fieldListStr,SysField.class);
if (fieldList.size() == 0) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
String tableName = fieldList.get(0).getTableName();
if (StringUtils.isEmpty(tableName)) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
String maintainId = null;
MaintainField maxVersion = maintainFieldService.getMaxVersion(tableName);
if (maxVersion != null) {
maintainId = maxVersion.getId();
}
MaintainField nowMaintain = null;
HttpSession session = request.getSession();
TUser user = (TUser) session.getAttribute("user");
String userId = user.getUserId();
Maintain dataMaintainMax = maintainService.getMaxVersion(tableName);
if (StringUtils.isEmpty(maintainId)) {
//创建新版本的字段
nowMaintain = flowsService.createNowVerion(tableName, maintainId, userId);
if (nowMaintain == null && dataMaintainMax != null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
}else {
Flows flows = flowsService.selectOne(new EntityWrapper().eq("business_id", maintainId));
if (flows != null){
//当前已审批
ActivitiStatus status = flows.getStatus();
if (status.equals(ActivitiStatus.open) || status.equals(ActivitiStatus.close)) {
nowMaintain = flowsService.createNowVerion(tableName, maintainId, userId);
}else {
return Result.error(new CodeMsg(7002, "当前字段版本正在审批,请待审批通过后再修改"));
}
}else {
//delete
fieldService.delete(new EntityWrapper().eq("maintain_field_id",maintainId));
nowMaintain = maintainFieldService.selectById(maintainId);
}
}
String nowMaintainId;
if (dataMaintainMax == null) {
nowMaintainId = null;
}else {
nowMaintainId = nowMaintain.getId();
}
if (StringUtils.isEmpty(nowMaintainId)) {
//第一次默认都清空
fieldService.delete(new EntityWrapper().isNull("maintain_field_id").eq("table_name", tableName));
}
for (int i = 0; i < fieldList.size(); i++) {
SysField sysField = fieldList.get(i);
sysField.setCreateTime(new Date()).setId(DbUtils.getUUID()).setMaintainFieldId(nowMaintainId).setOrderNo(i).insert();
}
if (nowMaintain != null) {
nowMaintain.insertOrUpdate();
}
return Result.success(null);
}
@RequestMapping(value = "/change", method = RequestMethod.POST)
public Result addOrUpdate(@RequestBody SysField sysField, HttpServletRequest request) {
String tableName = sysField.getTableName();
Maintain dataMaxVersion = maintainService.getMaxVersion(tableName);
boolean isInit = false;
if (dataMaxVersion == null) {
isInit = true;
}
if (!StringUtils.isEmpty(sysField.getId())) {
String id = sysField.getId();
SysField field = fieldService.selectById(id);
if (field != null){
Operate operate = field.getOperate();
if (operate != null) {
isInit = true;
}
}
}
Operate operate = sysField.getOperate();
if (isInit && (operate.equals(Operate.update) || operate.equals(Operate.create))) {
if (isInit) {
if (StringUtils.isEmpty(sysField.getId())) {
sysField.setId(DbUtils.getUUID());
}
sysField.setOperate(null);
boolean update = sysField.insertOrUpdate();
if (update) {
return Result.success(CodeMsg.SUCCESS);
}else {
return Result.error(CodeMsg.INSERT_ERROR);
}
}
}
if (isInit && operate.equals(Operate.delete)) {
String maintainFieldId = sysField.getMaintainFieldId();
boolean delete = sysField.deleteById();
if (delete) {
//删除后 判断是否有未提交的了
if (StringUtils.isEmpty(maintainFieldId)) {
return Result.success(CodeMsg.SUCCESS);
}
List fieldList = fieldService.selectList(new EntityWrapper().eq("maintain_field_id", maintainFieldId));
long count = fieldList.stream().filter(field -> field.getOperate() != null).count();
if (count == 0) {
//说明 当前版本没有修改的字段了
delete = fieldService.delete(new EntityWrapper().eq("maintain_field_id", maintainFieldId));
delete = maintainFieldService.deleteById(maintainFieldId);
if (delete) {
return Result.success(CodeMsg.SUCCESS);
}
}
return Result.success(CodeMsg.SUCCESS);
}else {
return Result.error(CodeMsg.DELETE_ERROR);
}
}
if (operate.equals(Operate.update)) {
String id = sysField.getId();
SysField field = fieldService.selectById(id);
if (field == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
boolean visiableEqual = field.getVisible().equals(sysField.getVisible());
boolean aliasEqual = field.getAlias().equals(sysField.getAlias());
String updateCode = field.getCode();
String preCode = sysField.getCode();
boolean codeEqual;
if (StringUtils.isEmpty(updateCode) && StringUtils.isEmpty(preCode)) {
codeEqual = true;
}else if(!StringUtils.isEmpty(updateCode) && !StringUtils.isEmpty(preCode) && updateCode.equalsIgnoreCase(preCode)){
codeEqual = true;
}else {
codeEqual = false;
}
String updateFormat = field.getFormat();
String preFormat = sysField.getFormat();
boolean formatterEqual;
if (StringUtils.isEmpty(updateFormat) && StringUtils.isEmpty(preFormat)) {
formatterEqual = true;
}else if(!StringUtils.isEmpty(updateFormat) && !StringUtils.isEmpty(preFormat) && updateFormat.equalsIgnoreCase(preFormat)){
formatterEqual = true;
}else {
formatterEqual = false;
}
if (visiableEqual && aliasEqual && codeEqual && formatterEqual) {
// 仅修改普通状态
boolean updated = fieldService.updateCommon(field, sysField);
if (!updated){
return Result.error(CodeMsg.UPDATE_ERROR);
}else {
field = fieldService.selectById(id);
return Result.success(field);
}
}
}
MaintainField maxVersion = maintainFieldService.getMaxVersion(sysField.getTableName());
String maintainId = null;
if(maxVersion != null) {
maintainId = maxVersion.getId();
}
MaintainField nowMaintain = null;
HttpSession session = request.getSession();
TUser user = (TUser) session.getAttribute("user");
String userId = user.getUserId();
if (operate.equals(Operate.create) || operate.equals(Operate.update)) {
String alias = sysField.getAlias();
List fieldByMaintainField;
if (nowMaintain == null) {
fieldByMaintainField = fieldService.getFieldByTable(sysField.getTableName());
}
else {
fieldByMaintainField = fieldService.getFieldByMaintainField(nowMaintain.getId());
}
long count = fieldByMaintainField.stream()
.filter(sysField1 -> !sysField1.getId().equalsIgnoreCase(sysField.getId()))
.filter(maintainField -> maintainField.getAlias().equals(alias)).count();
if (count > 0) {
//中文名相同
return Result.error(new CodeMsg(8001, "显示名称相同"));
}
}
if (StringUtils.isEmpty(maintainId)) {
//创建新版本的字段
nowMaintain = flowsService.createNowVerion(sysField.getTableName(), maintainId, userId);
if (nowMaintain == null) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}
//设置成新的版本
sysField.setMaintainFieldId(nowMaintain.getId());
}else {
Flows flows = flowsService.selectOne(new EntityWrapper().eq("business_id", maintainId));
if (flows != null){
//当前已审批
ActivitiStatus status = flows.getStatus();
Boolean refused = sysField.getRefused();
if (refused != null && refused && status.equals(ActivitiStatus.refuse)){
nowMaintain = maintainFieldService.selectById(maintainId);
}else {
if (status.equals(ActivitiStatus.open) || status.equals(ActivitiStatus.close)) {
nowMaintain = flowsService.createNowVerion(sysField.getTableName(), maintainId, userId);
sysField.setMaintainFieldId(nowMaintain.getId());
}else {
return Result.error(new CodeMsg(7002, "当前字段版本正在审批,请待审批通过后再修改"));
}
}
}else {
nowMaintain = maintainFieldService.selectById(maintainId);
}
}
if (nowMaintain == null) {
return Result.error(CodeMsg.OPERATR_ERROR);
}
//更新当前实体为新版本对应的id
if (operate.equals(Operate.create)) {
// create field
String alias = sysField.getAlias();
String chineseOrEnglishOrNumber = DbUtils.getChineseOrEnglishOrNumber(alias);
String field = WorkflowUtils.toFirstChar(chineseOrEnglishOrNumber);
SysField oneFieldByMaintainField = fieldService.getOneFieldByMaintainField(nowMaintain.getId(), field);
if (oneFieldByMaintainField != null){
//field 有相同的
field = field + Constant.UnderLine + DbUtils.getUUID(5);
}
sysField.setField(field);
sysField.setMaintainFieldId(nowMaintain.getId());
sysField.setId(DbUtils.getUUID());
sysField.setCreateTime(new Date());
sysField.insert();
// 主表中需新增字段
}
else if(operate.equals(Operate.update)) {
SysField relatedField = fieldService.getOneFieldByMaintainField(nowMaintain.getId(), sysField.getField());
if (relatedField == null) {
return Result.error(CodeMsg.OPERATR_ERROR);
}
Boolean refused = sysField.getRefused();
SysField field = fieldService.selectById(sysField.getId());
Operate perOperate = field.getOperate();
if (Operate.create.equals(perOperate) && refused != null && refused) {
sysField.setOperate(Operate.create);
}
sysField.setId(relatedField.getId());
sysField.setMaintainFieldId(relatedField.getMaintainFieldId());
sysField.setUpdateTime(new Date());
sysField.updateById();
}
else if(operate.equals(Operate.delete)) {
SysField relatedField = fieldService.getOneFieldByMaintainField(nowMaintain.getId(), sysField.getField());
if (relatedField == null) {
return Result.error(CodeMsg.OPERATR_ERROR);
}
sysField.setId(relatedField.getId());
sysField.setMaintainFieldId(relatedField.getMaintainFieldId());
Boolean refused = sysField.getRefused();
SysField field = fieldService.selectById(sysField.getId());
Operate perOperate = field.getOperate();
if (refused != null && refused && perOperate != null){
sysField.deleteById();
}else {
sysField.updateById();
}
}
nowMaintain.insertOrUpdate();
return Result.success(sysField);
}
}