MemberServiceImpl.java 11.3 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.company.AttendMembersDTO;
import com.subsidy.dto.member.*;
import com.subsidy.mapper.*;
import com.subsidy.model.*;
import com.subsidy.service.MemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.excel.ExcelUtil;
import com.subsidy.vo.member.AttendMembersVO;
import com.subsidy.vo.member.DepartMembersVO;
import com.subsidy.vo.member.GetAllVO;
import com.subsidy.vo.member.MemberAttendInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 学生表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-01-11
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, MemberDO> implements MemberService {

    @Autowired
    private MemberDepartmentMappingMapper memberDepartmentMappingMapper;

    @Autowired
    private CompanyMemberMappingMapper companyMemberMappingMapper;

    @Autowired
    private DepartmentDictMapper departmentDictMapper;

    @Autowired
    private JobDictMapper jobDictMapper;

    @Autowired
    private ClassMemberMappingMapper classMemberMappingMapper;

    @Autowired
    private JobMemberMappingMapper jobMemberMappingMapper;

    public IPage<DepartMembersVO> departMembers(DepartMembersDTO departMembersDTO) {
        Page pager = new Page(departMembersDTO.getPageNum(), departMembersDTO.getPageSize());
        return companyMemberMappingMapper.departMembers(pager, departMembersDTO.getCompanyId(), departMembersDTO.getUserName());
    }

    public void exportDepartMembers(DepartMembersDTO departMembersDTO) {
        Page pager = new Page(1, -1);
        IPage<DepartMembersVO> departMembersVOIPage = companyMemberMappingMapper.departMembers(pager, departMembersDTO.getCompanyId(), departMembersDTO.getUserName());
        List<DepartMembersVO> departMembersVOS = departMembersVOIPage.getRecords();
        ExcelUtil.writeExcel(departMembersVOS, DepartMembersVO.class);
    }

    public IPage<AttendMembersVO> attendMembers(AttendMembersDTO attendMembersDTO) {
        Page pager = new Page(attendMembersDTO.getPageNum(), attendMembersDTO.getPageSize());

        IPage<AttendMembersVO> attendMembersVOIPage = companyMemberMappingMapper.attendMembers(pager, attendMembersDTO.getCompanyId(), attendMembersDTO.getDepartmentId(), attendMembersDTO.getUserName());

        List<AttendMembersVO> getAllVOS = attendMembersVOIPage.getRecords();
        for (AttendMembersVO getAllVO : getAllVOS) {
            //获取这个人所在的部门
            List<DepartmentDictDO> departmentDictDOS = this.baseMapper.getDepartments(getAllVO.getId());
            getAllVO.setDepartmentDictDOS(departmentDictDOS);

            //获取这个人所在的岗位
            List<JobDictDO> jobDictDOS = jobDictMapper.queryMemberJobs(getAllVO.getId());
            getAllVO.setJobDictDOS(jobDictDOS);
        }
        return attendMembersVOIPage;
    }

    public List<MemberAttendInfoVO> memberAttendInfo(CompanyMemberMappingDO companyMemberMappingDO) {
        return companyMemberMappingMapper.memberAttendInfo(companyMemberMappingDO.getMemberId(), companyMemberMappingDO.getCompanyId());
    }


    public IPage<GetAllVO> getAll(GetAllDTO getAllDTO) {
        Page pager = new Page(getAllDTO.getPageNum(), getAllDTO.getPageSize());

        DepartmentDictDO departmentDictDO = departmentDictMapper.selectById(getAllDTO.getDepartmentId());

        IPage iPage = null;

        if (null != departmentDictDO.getParentId()) {
            iPage = this.baseMapper.getMembers(pager, getAllDTO.getDepartmentId(), getAllDTO.getUserName(), getAllDTO.getStatus(), getAllDTO.getStartDate(), getAllDTO.getEndDate());
        } else {
            iPage = this.baseMapper.getCompanyMember(pager, departmentDictDO.getCompanyId(), getAllDTO.getUserName(), getAllDTO.getStatus(), getAllDTO.getStartDate(), getAllDTO.getEndDate());
        }

        List<GetAllVO> getAllVOS = iPage.getRecords();
        for (GetAllVO getAllVO : getAllVOS) {
            //获取这个人所在的部门
            List<DepartmentDictDO> departmentDictDOS = this.baseMapper.getDepartments(getAllVO.getId());
            getAllVO.setDepartmentDictDOS(departmentDictDOS);

            //获取这个人所在的岗位
            List<JobDictDO> jobDictDOS = jobDictMapper.queryMemberJobs(getAllVO.getId());
            getAllVO.setJobDictDOS(jobDictDOS);
        }
        return iPage;
    }


    @Transactional(rollbackFor = Exception.class)
    public String deleteMember(MemberDO memberDO) {

        this.baseMapper.deleteById(memberDO.getId());
        classMemberMappingMapper.delete(new QueryWrapper<ClassMemberMappingDO>()
                .lambda()
                .eq(ClassMemberMappingDO::getMemberId, memberDO.getId()));
        memberDepartmentMappingMapper.delete(new QueryWrapper<MemberDepartmentMappingDO>()
                .lambda()
                .eq(MemberDepartmentMappingDO::getMemberId, memberDO.getId()));
        return ConstantUtils.DELETE_SUCCESS;
    }

    public String addMember(AddMemberDTO addMemberDTO) {

        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getTelephone, addMemberDTO.getTelephone())
                .eq(MemberDO::getMemberType, 2));

        if (count > 0) {
            throw new HttpException(20002);
        }

        Integer count2 = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getIdCard, addMemberDTO.getIdCard())
                .eq(MemberDO::getMemberType, 2));

        if (count2 > 0) {
            throw new HttpException(20002);
        }

        List<MemberDO> memberDOS = companyMemberMappingMapper.companyAccountMembers(addMemberDTO.getCompanyId(), addMemberDTO.getAccountName());

        if (memberDOS.size() > 0) {
            throw new HttpException(20002);
        }

        MemberDO memberDO = new MemberDO();
        BeanUtils.copyProperties(addMemberDTO, memberDO);
        memberDO.setPassword("Ykhl123456");
        memberDO.setStatus("启用");
        memberDO.setFirstLogin(0);
        memberDO.setMemberType(2);
        this.baseMapper.insert(memberDO);
        //公司赋值
        CompanyMemberMappingDO companyMemberMappingDO = new CompanyMemberMappingDO();
        companyMemberMappingDO.setCompanyId(addMemberDTO.getCompanyId());
        companyMemberMappingDO.setMemberId(memberDO.getId());
        companyMemberMappingDO.setMemberStatus(1);
        companyMemberMappingMapper.insert(companyMemberMappingDO);


        if (null != addMemberDTO.getDepartmentIds()) {
            //部门
            List<Long> longs = addMemberDTO.getDepartmentIds();

            for (Long lg : longs) {
                MemberDepartmentMappingDO memberDepartmentMappingDO = new MemberDepartmentMappingDO();
                memberDepartmentMappingDO.setMemberId(memberDO.getId());
                memberDepartmentMappingDO.setDepartmentId(lg);
                memberDepartmentMappingDO.setMemberStatus(1);
                memberDepartmentMappingMapper.insert(memberDepartmentMappingDO);
            }
        }


        if (null != addMemberDTO.getJobIds()) {
            jobMemberMappingMapper.delete(new QueryWrapper<JobMemberMappingDO>()
                    .lambda()
                    .eq(JobMemberMappingDO::getMemberId, addMemberDTO.getId()));

            //岗位
            List<Long> longs = addMemberDTO.getJobIds();
            for (Long lg : longs) {
                JobMemberMappingDO jobMemberMappingDO = new JobMemberMappingDO();
                jobMemberMappingDO.setMemberId(memberDO.getId());
                jobMemberMappingDO.setJobId(lg);
                jobMemberMappingMapper.insert(jobMemberMappingDO);
            }
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateMember(AddMemberDTO addMemberDTO) {

        Integer count = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getTelephone, addMemberDTO.getTelephone())
                .ne(MemberDO::getId, addMemberDTO.getId()));

        if (count > 0) {
            throw new HttpException(20002);
        }

        Integer count1 = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getIdCard, addMemberDTO.getIdCard())
                .ne(MemberDO::getId, addMemberDTO.getId()));

        if (count1 > 0) {
            throw new HttpException(20002);
        }

        MemberDO memberDO = new MemberDO();
        BeanUtils.copyProperties(addMemberDTO, memberDO);
        this.baseMapper.updateById(memberDO);

        if (null != addMemberDTO.getDepartmentIds()) {
            memberDepartmentMappingMapper.delete(new QueryWrapper<MemberDepartmentMappingDO>()
                    .lambda()
                    .eq(MemberDepartmentMappingDO::getMemberId, addMemberDTO.getId()));

            //部门
            List<Long> longs = addMemberDTO.getDepartmentIds();
            for (Long lg : longs) {
                MemberDepartmentMappingDO memberDepartmentMappingDO = new MemberDepartmentMappingDO();
                memberDepartmentMappingDO.setMemberId(memberDO.getId());
                memberDepartmentMappingDO.setDepartmentId(lg);
                memberDepartmentMappingDO.setMemberStatus(1);
                memberDepartmentMappingMapper.insert(memberDepartmentMappingDO);
            }
        }

        if (null != addMemberDTO.getJobIds()) {
            jobMemberMappingMapper.delete(new QueryWrapper<JobMemberMappingDO>()
                    .lambda()
                    .eq(JobMemberMappingDO::getMemberId, addMemberDTO.getId()));

            //岗位
            List<Long> longs = addMemberDTO.getJobIds();
            for (Long lg : longs) {
                JobMemberMappingDO jobMemberMappingDO = new JobMemberMappingDO();
                jobMemberMappingDO.setMemberId(memberDO.getId());
                jobMemberMappingDO.setJobId(lg);
                jobMemberMappingMapper.insert(jobMemberMappingDO);
            }
        }

        return ConstantUtils.SET_SUCCESS;
    }


    public String changeDepartments(ChangeDepartmentsDTO changeDepartmentsDTO) {

        List<Long> memberIds = changeDepartmentsDTO.getMemberIds();

        //清掉以前的部门
        for (Long lg : memberIds) {
            memberDepartmentMappingMapper.cancelMember(lg);
        }

        //加入新的部门
        List<Long> depIds = changeDepartmentsDTO.getDesDepId();
        for (Long lg : memberIds) {
            for (Long depId : depIds) {
                MemberDepartmentMappingDO memberDepartmentMappingDO = new MemberDepartmentMappingDO();
                memberDepartmentMappingDO.setMemberId(lg);
                memberDepartmentMappingDO.setDepartmentId(depId);
                memberDepartmentMappingDO.setMemberStatus(1);
                memberDepartmentMappingMapper.insert(memberDepartmentMappingDO);
            }
        }
        return ConstantUtils.SET_SUCCESS;
    }


}