package com.highdatas.mdm.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.highdatas.mdm.entity.MasterAuthor;
import com.highdatas.mdm.entity.MasterAuthorDetail;
import com.highdatas.mdm.entity.TUser;
import com.highdatas.mdm.entity.TUserRole;
import com.highdatas.mdm.pojo.CodeMsg;
import com.highdatas.mdm.pojo.MasterAuthorType;
import com.highdatas.mdm.pojo.Result;
import com.highdatas.mdm.service.IMasterAuthorDetailService;
import com.highdatas.mdm.service.IMasterAuthorService;
import com.highdatas.mdm.service.ITUserRoleService;
import com.highdatas.mdm.util.Constant;
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.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 前端控制器
*
*
* @author kimi
* @since 2020-03-23
*/
@RestController
@RequestMapping("/masterAuthor")
public class MasterAuthorController {
@Autowired
IMasterAuthorService authorService;
@Autowired
IMasterAuthorDetailService authorDetailService;
@Autowired
ITUserRoleService userRoleService;
public static final String masterAuthorType = "masterAuthorType";
public static final String masterId = "masterId";
public static final String characterId = "characterId";
public static final String fields = "fields";
private String maintainFieldId = "maintainFieldId";
@RequestMapping(value = "/addOrUpdate", method = RequestMethod.POST)
public Result deleteModel(@RequestBody MasterAuthor masterAuthor) {
if (!StringUtils.isEmpty(masterAuthor.getId())) {
masterAuthor.setUpdateTime(new Date());
}else {
masterAuthor.setId(DbUtils.getUUID()).setCreateTime(new Date());
}
//delete pre
boolean delete = authorDetailService.delete(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId()));
if (!delete){
return Result.error(CodeMsg.DELETE_ERROR);
}
for (MasterAuthorDetail detail : masterAuthor.getFields()) {
detail.setId(DbUtils.getUUID()).setParentId(masterAuthor.getId()).insert();
}
boolean b = masterAuthor.insertOrUpdate();
if (b) {
return Result.success(masterAuthor);
}else {
return Result.error(CodeMsg.UPDATE_ERROR);
}
}
@RequestMapping(value = "/get/{characterId}", method = RequestMethod.GET)
public Result get(@PathVariable String characterId, @RequestParam MasterAuthorType type, HttpServletRequest request){
List masterAuthorList = authorService.selectList(new EntityWrapper().eq("type", type.name()).eq("character_id", characterId));
if (type.equals(MasterAuthorType.role) && masterAuthorList.isEmpty()) {
return Result.error(CodeMsg.ERROR_PARAMS_NOT_MATHED);
}else if (type.equals(MasterAuthorType.user) && masterAuthorList.isEmpty()){
//user 获取角色 多脚色混合
TUser user = DbUtils.getUser(request);
String userId = user.getUserId();
List tUserRoles = userRoleService.selectList(new EntityWrapper().eq(Constant.USERID, userId));
List roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
return authorService.merageRoleAuthor(roleIdList);
}
for (MasterAuthor masterAuthor : masterAuthorList) {
List masterAuthorDetails = authorDetailService
.selectList(new EntityWrapper()
.eq(Constant.PARENT_ID, masterAuthor.getId()));
masterAuthor.setFields(masterAuthorDetails);
}
return Result.success(masterAuthorList);
}
@RequestMapping(value = "/delete/{characterId}", method = RequestMethod.GET)
public Result delete(@PathVariable String characterId, @RequestParam MasterAuthorType type){
List masterAuthorList = authorService.selectList(new EntityWrapper()
.eq(Constant.TYPE, type).eq(this.characterId, characterId));
if (masterAuthorList.isEmpty()) {
return Result.success(null);
}
boolean delete = false;
for (MasterAuthor masterAuthor : masterAuthorList) {
delete = authorDetailService.delete(new EntityWrapper().eq(Constant.PARENT_ID, masterAuthor.getId()));
if (delete) {
delete = masterAuthor.deleteById();
}
}
if (delete) {
return Result.success(CodeMsg.DELETE_SUCCESS);
}else {
return Result.error(CodeMsg.DELETE_ERROR);
}
}
@RequestMapping(value = "/addRole/{roleId}", method = RequestMethod.GET)
public Result addRole(@PathVariable String roleId, HttpServletRequest request){
TUser user = DbUtils.getUser(request);
String userId = user.getUserId();
List tUserRoles = userRoleService.selectList(new EntityWrapper().eq(Constant.USERID, userId));
List roleIdList = tUserRoles.stream().map(tUserRole -> tUserRole.getRoleId()).collect(Collectors.toList());
roleIdList.add(roleId);
return authorService.merageRoleAuthor(roleIdList);
}
}