package com.moral.api.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.moral.api.entity.Organization;
import com.moral.api.mapper.OrganizationMapper;
import com.moral.api.pojo.dto.organization.OrganizationDTO;
import com.moral.api.pojo.dto.organization.OrganizationQueryDTO;
import com.moral.api.pojo.form.organization.OrganizationDeleteForm;
import com.moral.api.pojo.form.organization.OrganizationInsertForm;
import com.moral.api.pojo.form.organization.OrganizationQueryForm;
import com.moral.api.pojo.form.organization.OrganizationUpdateForm;
import com.moral.api.service.OrganizationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.ResponseCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import java.util.List;
/**
*
* 组织表 服务实现类
*
*
* @author moral
* @since 2021-03-09
*/
@Service
public class OrganizationServiceImpl extends ServiceImpl implements OrganizationService {
@Autowired
OrganizationMapper organizationMapper;
/**
* @Description: 添加客户组织
* @Param: [organizationInsertForm]
* @return: void
* @Author: 陈凯裕
* @Date: 2021/3/22
*/
@Override
@Transactional
public OrganizationDTO insertOrganization(OrganizationInsertForm organizationInsertForm) {
OrganizationDTO organizationDTO = new OrganizationDTO();
QueryWrapper queryWrapper = new QueryWrapper<>();
//form转换entity
Organization organization = organizationInsertForm.formConvertEntity();
//查询组织名称是否已经存在
Organization existOrganization = new Organization();
existOrganization.setName(organization.getName());
existOrganization.setIsDelete(Constants.NOT_DELETE);
queryWrapper.setEntity(existOrganization);
List existOrganizations = organizationMapper.selectList(queryWrapper);
if (!ObjectUtils.isEmpty(existOrganizations)) {
organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_EXIST.getCode());
organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_EXIST.getMsg());
return organizationDTO;
}
//查询父组织是否存在
Integer parentId = organization.getParentId();
Organization parentOrganization = new Organization();
if (!ObjectUtils.isEmpty(parentId)) {
parentOrganization.setId(parentId);
parentOrganization.setIsDelete(Constants.NOT_DELETE);
queryWrapper.setEntity(parentOrganization);
parentOrganization = organizationMapper.selectOne(queryWrapper);
if (ObjectUtils.isEmpty(parentOrganization)) {
organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
return organizationDTO;
}
}
//插入组织
organizationMapper.insert(organization);
//封装DTO信息
organizationDTO.setParentOrganization(parentOrganization);
organizationDTO.setOrganization(organization);
organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return organizationDTO;
}
/**
* @Description: 更新客户组织
* @Param: [organizationUpdateForm]
* @return: com.moral.api.pojo.dto.organization.OrganizationDTO
* @Author: 陈凯裕
* @Date: 2021/3/24
*/
@Override
@Transactional
public OrganizationDTO updateOrganization(OrganizationUpdateForm organizationUpdateForm) {
OrganizationDTO organizationDTO = new OrganizationDTO();
//form转entity
Organization organization = organizationUpdateForm.formConvertEntity();
//查询组织是否存在
QueryWrapper existWrapper = new QueryWrapper<>();
Organization existOrganization = new Organization();
existOrganization.setId(organization.getId());
existOrganization.setIsDelete(Constants.NOT_DELETE);
existWrapper.setEntity(existOrganization);
existOrganization = organizationMapper.selectOne(existWrapper);
if(ObjectUtils.isEmpty(existOrganization)){
organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
return organizationDTO;
}
//如果更改了父组织,查询父组织是否存在
Integer parentId = organization.getParentId();
Organization parentOrganization = new Organization();
if (!ObjectUtils.isEmpty(parentId)&&parentId!=0) {
QueryWrapper existParentWrapper = new QueryWrapper<>();
parentOrganization.setId(parentId);
parentOrganization.setIsDelete(Constants.NOT_DELETE);
existParentWrapper.setEntity(parentOrganization);
parentOrganization = organizationMapper.selectOne(existParentWrapper);
if (ObjectUtils.isEmpty(parentOrganization)) {
organizationDTO.setCode(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getCode());
organizationDTO.setMsg(ResponseCodeEnum.ORGANIZATION_PARENT_NOT_EXIST.getMsg());
return organizationDTO;
}
}
//更新组织
organizationMapper.updateById(organization);
//获取更新后的组合
organization = organizationMapper.selectById(organization.getId());
//封装DTO信息
organizationDTO.setParentOrganization(parentOrganization);
organizationDTO.setOrganization(organization);
organizationDTO.setCode(ResponseCodeEnum.SUCCESS.getCode());
organizationDTO.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return organizationDTO;
}
/**
* @Description: 删除客户组织
* @Param: [organizationDeleteForm]
* @return: com.moral.api.pojo.dto.organization.OrganizationDTO
* @Author: 陈凯裕
* @Date: 2021/3/25
*/
@Override
@Transactional
public OrganizationDTO deleteOrganization(OrganizationDeleteForm form) {
OrganizationDTO dto = new OrganizationDTO();
//取参
Integer id = form.getOrganizationId();
//查询组织是否存在
Organization existOrganization = new Organization();
existOrganization.setIsDelete(Constants.NOT_DELETE);
existOrganization.setId(id);
QueryWrapper queryExistWrapper = new QueryWrapper();
queryExistWrapper.setEntity(existOrganization);
existOrganization = organizationMapper.selectOne(queryExistWrapper);
if(ObjectUtils.isEmpty(existOrganization)){
dto.setCode(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getCode());
dto.setMsg(ResponseCodeEnum.ORGANIZATION_NOT_EXIST.getMsg());
return dto;
}
//逻辑删除组织
UpdateWrapper deleteWrapper = new UpdateWrapper();
deleteWrapper.eq("id",id);
deleteWrapper.set("is_delete",Constants.DELETE);
organizationMapper.update(null,deleteWrapper);
//判断是否删除所有子组织,如果不删除则将子组织parentId赋0
if(form.getDeleteChildren().equals(Constants.DELETE_CHILDREN_ORG)){
UpdateWrapper deleteChildrenWrapper = new UpdateWrapper();
deleteChildrenWrapper.eq("parent_id",id);
deleteChildrenWrapper.set("is_delete",Constants.DELETE);
organizationMapper.update(null,deleteChildrenWrapper);
}else{
UpdateWrapper updateChildrenWrapper = new UpdateWrapper();
updateChildrenWrapper.eq("parent_id",id);
updateChildrenWrapper.set("parent_id",0);
organizationMapper.update(null,updateChildrenWrapper);
}
dto.setOrganization(existOrganization);
dto.setCode(ResponseCodeEnum.SUCCESS.getCode());
dto.setMsg(ResponseCodeEnum.SUCCESS.getMsg());
return dto;
}
/**
* @Description: 查询客户组织
* @Param: [organizationQueryForm]
* @return: com.moral.api.pojo.dto.organization.OrganizationQueryDTO
* @Author: 陈凯裕
* @Date: 2021/3/25
*/
@Override
public OrganizationQueryDTO queryOrganization(OrganizationQueryForm organizationQueryForm) {
OrganizationQueryDTO dto = new OrganizationQueryDTO();
//取参
Organization organization = organizationQueryForm.formConvertEntity();
Integer pageCount = organizationQueryForm.getPage();
Integer size = organizationQueryForm.getSize();
String order = organizationQueryForm.getOrder();
String orderType = organizationQueryForm.getOrderType();
//查询用户
Page page = new Page<>(pageCount,size);
QueryWrapper queryWrapper = new QueryWrapper<>();
return null;
}
}