MemberServiceImpl.java 10.6 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.administer.DownloadFailMembersDTO;
import com.subsidy.dto.administer.ImportMemberVO;
import com.subsidy.dto.member.AddMemberDTO;
import com.subsidy.dto.member.ImportMemberDTO;
import com.subsidy.dto.member.QueryMembersDTO;
import com.subsidy.mapper.GroupMemberMappingMapper;
import com.subsidy.mapper.LaborGroupDictMapper;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.mapper.ProjectMemberMappingMapper;
import com.subsidy.model.GroupMemberMappingDO;
import com.subsidy.model.LaborGroupDictDO;
import com.subsidy.model.MemberDO;
import com.subsidy.model.ProjectMemberMappingDO;
import com.subsidy.service.MemberService;
import com.subsidy.util.excel.ExcelUtil;
import com.subsidy.vo.member.DetailVO;
import com.subsidy.vo.member.ImportMemberResultVO;
import com.subsidy.vo.member.QueryMembersVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

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

    @Autowired
    private ProjectMemberMappingMapper projectMemberMappingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private GroupMemberMappingMapper groupMemberMappingMapper;

    @Autowired
    private LaborGroupDictMapper laborGroupDictMapper;

    public void addMember(AddMemberDTO addMemberDTO) {

        //查看手机号是否存在
        int count = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getTelephone, addMemberDTO.getTelephone())
                .eq(MemberDO::getMemberType, 3));

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

        //查看身份证号是否存在
        int count1 = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getIdCard, addMemberDTO.getIdCard())
                .eq(MemberDO::getMemberType, 3));
        if (count1 > 0) {
            throw new HttpException(20002);
        }
        MemberDO memberDO = new MemberDO();
        BeanUtils.copyProperties(addMemberDTO, memberDO);
        memberDO.setMemberType(3);
        this.baseMapper.insert(memberDO);

        GroupMemberMappingDO groupMemberMappingDO = new GroupMemberMappingDO();
        groupMemberMappingDO.setGroupId(addMemberDTO.getGroupId());
        groupMemberMappingDO.setMemberId(memberDO.getId());
        groupMemberMappingMapper.insert(groupMemberMappingDO);
    }

    public void updateMember(AddMemberDTO addMemberDTO) {

        //查看手机号是否存在
        int count = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getTelephone, addMemberDTO.getTelephone())
                .ne(MemberDO::getId, addMemberDTO.getId())
                .eq(MemberDO::getMemberType, 3));

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

        //查看身份证号是否存在
        int count1 = this.baseMapper.selectCount(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getIdCard, addMemberDTO.getIdCard())
                .ne(MemberDO::getId, addMemberDTO.getId())
                .eq(MemberDO::getMemberType, 3));
        if (count1 > 0) {
            throw new HttpException(20002);
        }
        MemberDO memberDO = new MemberDO();
        BeanUtils.copyProperties(addMemberDTO, memberDO);
        this.baseMapper.updateById(memberDO);

    }

    public void deleteMember(MemberDO memberDO) {
        this.baseMapper.deleteById(memberDO.getId());
    }

    public IPage<QueryMembersVO> queryMembers(QueryMembersDTO queryMembersDTO) {

        LaborGroupDictDO laborGroupDictDO = laborGroupDictMapper.selectById(queryMembersDTO.getGroupId());

        Page page = new Page(queryMembersDTO.getPageNum(), queryMembersDTO.getPageSize());

        IPage<QueryMembersVO> queryMembers = this.baseMapper.queryMembers(page, queryMembersDTO.getGroupId(), queryMembersDTO.getUserInfo());

        List<QueryMembersVO> queryMembersVOS = queryMembers.getRecords();
        for (QueryMembersVO queryMembersVO : queryMembersVOS) {
            //入职状态
            List<ProjectMemberMappingDO> memberMappingDOS = projectMemberMappingMapper.selectList(new QueryWrapper<ProjectMemberMappingDO>()
                    .lambda()
                    .eq(ProjectMemberMappingDO::getMemberId, queryMembersVO.getId())
                    .orderByDesc(ProjectMemberMappingDO::getId));

            if (memberMappingDOS.size() > 0) {
                if (1 == memberMappingDOS.get(0).getStatus()) {
                    queryMembersVO.setFireStatus("入职");
                } else {
                    queryMembersVO.setFireStatus("离职");
                }
            } else {
                queryMembersVO.setFireStatus("离职");
            }
        }
        return queryMembers;
    }

    public List<DetailVO> detail(GroupMemberMappingDO groupMemberMappingDO) {
        return projectMemberMappingMapper.detail(groupMemberMappingDO.getMemberId());
    }

    //    @Transactional(rollbackFor = Exception.class)
    public ImportMemberResultVO importMember(Long groupId, MultipartFile multipartFile) throws Exception {

        ImportMemberResultVO importMemberResultVO = new ImportMemberResultVO();

        List<ImportMemberVO> failMembers = new ArrayList<>();

        int successCount = 0;
        int failCount = 0;
        List<ImportMemberDTO> importMemberDTOS = ExcelUtil.readExcelWithoutTitle(null, ImportMemberDTO.class, multipartFile);
        for (ImportMemberDTO s : importMemberDTOS) {
            try {

                MemberDO memberDel = memberMapper.selectOne(new QueryWrapper<MemberDO>()
                        .lambda()
                        .eq(MemberDO::getTelephone, s.getTelephone())
                        .eq(MemberDO::getMemberType, 3));

                MemberDO memberIdCard = memberMapper.selectOne(new QueryWrapper<MemberDO>()
                        .lambda()
                        .eq(MemberDO::getIdCard, s.getIdCard())
                        .eq(MemberDO::getMemberType, 3));

                //日期格式
                SimpleDateFormat d = null;
                Date date = null;

                if (StringUtils.isEmpty(s.getUserName()) || StringUtils.isEmpty(s.getTelephone()) || StringUtils.isEmpty(s.getGender()) || StringUtils.isEmpty(s.getIdCard()) || StringUtils.isEmpty(s.getPayrollAccount())) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("必填字段");
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != s.getIdCard() && s.getIdCard().length() != 18) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("身份证位数不正确");
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != s.getTelephone() && s.getTelephone().length() != 11) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("手机号位数不正确");
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != memberDel) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("手机号重复");
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != memberIdCard) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("身份证重复");
                    failMembers.add(importMemberVO);
                    failCount++;
                } else {
                    MemberDO memberDO1 = new MemberDO();
                    memberDO1.setUserName(s.getUserName());
                    memberDO1.setGender(s.getGender());
                    memberDO1.setFirstLogin(0);
                    memberDO1.setTelephone(s.getTelephone());
                    memberDO1.setIdCard(s.getIdCard().toUpperCase());
                    memberDO1.setStatus("启用");
                    memberDO1.setPayrollAccount(s.getPayrollAccount());
                    memberDO1.setBank(s.getBank());
                    memberDO1.setMemberType(3);
                    memberMapper.insert(memberDO1);

                    //人和群组的映射
                    GroupMemberMappingDO groupMemberMappingDO = new GroupMemberMappingDO();
                    groupMemberMappingDO.setMemberId(memberDO1.getId());
                    groupMemberMappingDO.setGroupId(groupId);
                    groupMemberMappingMapper.insert(groupMemberMappingDO);
                    successCount++;

                }
            } catch (Exception e) {
                ImportMemberVO importMemberVO = new ImportMemberVO();
                BeanUtils.copyProperties(s, importMemberVO);
                importMemberVO.setReason("其他原因");
                failCount++;
                failMembers.add(importMemberVO);
            }
        }
        importMemberResultVO.setImportMemberVOS(failMembers);
        importMemberResultVO.setSuccessCount(successCount);
        importMemberResultVO.setFailCount(failCount);
        return importMemberResultVO;
    }

    public void downloadFailMembers(DownloadFailMembersDTO downloadFailMembersDTO) {
        List<ImportMemberVO> importMemberVOS = downloadFailMembersDTO.getImportMemberVOS();
        ExcelUtil.writeExcel(importMemberVOS, ImportMemberVO.class);
    }


}