张晓波
2023-09-19 164694c47c35d6654df69b533e8dbf8b5423efc5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.thhy.staff.modules.biz.dorm.service.impl;
 
import com.alibaba.nacos.api.utils.StringUtils;
import com.thhy.general.common.BasicStatus;
import com.thhy.general.config.SysUserInfo;
import com.thhy.general.exception.BasicException;
import com.thhy.general.utils.DeepCopyUtils;
import com.thhy.general.utils.UserInfoUtils;
import com.thhy.staff.modules.biz.dorm.entity.Dorm;
import com.thhy.staff.modules.biz.dorm.entity.DormDto;
import com.thhy.staff.modules.biz.dorm.entity.DormListVo;
import com.thhy.staff.modules.biz.dorm.mapper.DormMapper;
import com.thhy.staff.modules.biz.dorm.service.DormService;
import com.thhy.staff.modules.biz.dormuser.entity.DormUser;
import com.thhy.staff.modules.biz.dormuser.entity.DormUserVo;
import com.thhy.staff.modules.biz.dormuser.mapper.DormUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.io.Serializable;
import java.util.Date;
import java.util.List;
 
/**
 *  服务实现类
 * @author zhang_xiao_bo
 * @since 2023-04-03 12:49:45
 */
@Service
public class DormServiceImpl implements DormService {
 
 
 
    @Autowired
    private DormMapper dormMapper;
 
    @Autowired
    private DormUserMapper dormUserMapper;
 
    public Dorm get(Serializable id){
        return dormMapper.queryById(id);
    }
 
    public List<DormListVo> findList(DormDto dormDto){
        SysUserInfo userInfo = UserInfoUtils.getInstance().getUserInfo();
        if(dormDto==null)dormDto=new DormDto();
        dormDto.setCompanyId(userInfo.getCompanyId());
        List<DormListVo> list = dormMapper.findList(dormDto);
        for (DormListVo dormListVo :list){
            List<DormUserVo> dormUserVoList = dormUserMapper.findDormUserList(dormListVo.getDormId());
            dormListVo.setDormUserVos(dormUserVoList);
        }
        return list;
    }
 
    /**
     * 增加和修改
     * @param dormDto
     */
    @Transactional
    public void addDorm(DormDto dormDto){
        SysUserInfo sysUserInfo = UserInfoUtils.getInstance().getUserInfo();
        String userIds = dormDto.getUserIds();
        Dorm dorm = new Dorm();
        DeepCopyUtils.copy(dormDto,dorm);
        List<Dorm> dormList = dormMapper.queryDormIdByRoom(dormDto.getRoomId());
        if(dorm.getDormId() == null){
            //增加操作
            if(dormList.size()>0){
                throw new BasicException(BasicStatus.DORM_IS_EXIST);
            }
            dorm.setCreateUser(sysUserInfo.getUserId());
            dormMapper.insert(dorm);
        }else{
            //修改操作
            if(dormList.size()>1){
                throw new BasicException(BasicStatus.DORM_DATA_IS_ERROR);
            }else if(dormList.size()==1){
                if(!dormList.get(0).getDormId().equals(dormDto.getDormId())){
                    throw new BasicException(BasicStatus.DORM_IS_EXIST);
                }
            }
            dorm.setUpdateUser(sysUserInfo.getUserId());
            dorm.setUpdateTime(new Date());
            dormMapper.update(dorm);
        }
        if(!StringUtils.isEmpty(userIds)){
            String[] userIdArray = new String[]{userIds};
            if(userIds.contains(",")){
                userIdArray = userIds.split(",");
            }
            if(dormDto.getBedNum()<userIdArray.length){
                throw new BasicException(BasicStatus.PERSON_GT_BEDNUM); //入住人数不能多于床位数
            }
            dormUserMapper.deletelogic(dormDto.getDormId());
            for(String userId : userIdArray){
                dormUserMapper.insert(new DormUser(dorm.getDormId(),userId,sysUserInfo.getUserId()));
            }
        }
    }
 
    /**
     * 修改
     * @param dorm
     */
    public void update(Dorm dorm){
        dormMapper.update(dorm);
    }
 
    /**
     * 删除
     * @param dormId
     */
    public void delete(Serializable dormId){
        dormMapper.deletelogic(dormId);
    }
 
    @Override
    public DormDto getDormInfo(Dorm dorm) {
        DormDto dormDto = dormMapper.queryByIdHasUser(dorm.getDormId());
        List<DormUserVo> dormUserVoList = dormUserMapper.findDormUserList(dorm.getDormId());
        dormDto.setDormUserVoList(dormUserVoList);
        return dormDto;
    }
}