forked from kidgrow-microservices-platform

zhaoxiaohao
2020-04-10 665c0a06d0bca5a22fb42406c465d736f876b04e
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.kidgrow.usercenter.service.impl;
 
import com.kidgrow.common.constant.SecurityConstants;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.model.SysUser;
import com.kidgrow.usercenter.service.ISysUserService;
import com.kidgrow.usercenter.vo.SysDoctorDto;
import com.kidgrow.usercenter.vo.SysDoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.kidgrow.common.model.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.common.service.impl.SuperServiceImpl;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.collections4.MapUtils;
import lombok.extern.slf4j.Slf4j;
 
import com.kidgrow.usercenter.model.SysDoctor;
import com.kidgrow.usercenter.mapper.SysDoctorMapper;
import com.kidgrow.usercenter.service.ISysDoctorService;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @version 1.0
 * @Description: <br>
 * @Project: 用户中心<br>
 * @CreateDate: Created in 2020-04-02 14:02:50 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@Slf4j
@Service
public class SysDoctorServiceImpl extends SuperServiceImpl<SysDoctorMapper, SysDoctor> implements ISysDoctorService {
    /**
     * 列表
     *
     * @param params
     * @return
     */
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private PasswordEncoder passwordEncoder;
 
    @Override
    public PageResult<SysDoctorVo> findList(Map<String, Object> params) {
        Page<SysDoctor> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<SysDoctor> list = baseMapper.findList(page, params);
        List<SysDoctorVo> listvo = new ArrayList<>();
        list.forEach(e -> {
            SysDoctorVo vo = new SysDoctorVo();
            BeanCopier beanCopier = BeanCopier.create(SysDoctor.class, SysDoctorVo.class, false);
            beanCopier.copy(e, vo, null);
            //查询用户的登录账号;
            if (e.getUserId() != null) {
                SysUser sysUser = iSysUserService.getById(e.getUserId());
                if (sysUser != null) {
                    vo.setUsername(sysUser.getUsername());
                }
            }
            listvo.add(vo);
        });
        return PageResult.<SysDoctorVo>builder().data(listvo).code(0).count(page.getTotal()).build();
    }
 
    /**
     * 根据SysDoctor对象当做查询条件进行查询
     *
     * @param sysDoctor
     * @return SysDoctor
     */
    @Override
    public SysDoctor findByObject(SysDoctor sysDoctor) {
        return baseMapper.findByObject(sysDoctor);
    }
 
    @Override
    public boolean delete(Long id) {
        //查询user表
        SysDoctor sysDoctor = baseMapper.selectById(id);
        if (sysDoctor != null && sysDoctor.getUserId() != null) {
            boolean b = iSysUserService.delUser(sysDoctor.getUserId());
        }
        sysDoctor.setIsDel(true);
        int i = baseMapper.deleteById(id);
        return true;
    }
 
    @Override
    public ResultBody enable(Map<String, Object> params) {
        Long aLong = MapUtils.getLong(params,"id");
        SysDoctor sysDoctor = baseMapper.selectById(aLong);
        if (sysDoctor != null && sysDoctor.getUserId() != null) {
            SysUser byId = iSysUserService.getById(sysDoctor.getUserId());
            if(byId!=null){
                byId.setEnabled(true);
                iSysUserService.updateById(byId);
            }
        }
        if(sysDoctor!=null){
            sysDoctor.setEnabled(true);
            baseMapper.updateById(sysDoctor);
        }else {
            return ResultBody.failed("禁用失败");
        }
        return ResultBody.ok(0,"禁用成功");
    }
 
    @Override
    @Transactional
    public boolean saveOrUpdateSer(SysDoctorDto sysDoctor, HttpServletRequest request) {
        String id = request.getHeader(SecurityConstants.USER_ID_HEADER);
        if(null==sysDoctor){
           return  false;
        }else {
            if (sysDoctor.getId()==null) {
                //保存
                SysUser sysUser=new SysUser();
                sysUser.setUsername(sysDoctor.getUsername());
                sysUser.setPassword(passwordEncoder.encode(sysDoctor.getPassword()));
                sysUser.setNickname(sysDoctor.getHospitalName());
                sysUser.setHeadImgUrl(sysDoctor.getDoctorLogo());
                sysUser.setMobile(sysDoctor.getDoctorTel());
                sysUser.setType("doctor");
                sysUser.setTenantId("hospital");
                sysUser.setCreateTime(new Date());
                SysUser byId = iSysUserService.getById(id);
                if(byId!=null){
                    sysUser.setCreateUserId(byId.getId());
                    sysUser.setCreateUserName(byId.getUsername());
                }
                iSysUserService.save(sysUser);
                //保存doctor的数据
                SysDoctor sysDoc=new SysDoctor();
                BeanCopier beanCopier = BeanCopier.create(SysDoctorDto.class, SysDoctor.class, false);
                beanCopier.copy(sysDoctor,sysDoc,null);
                sysDoc.setUserId(sysUser.getId());
                baseMapper.insert(sysDoc);
            }else {
                //更新
                baseMapper.updateById(sysDoctor);
            }
        }
        return true;
    }
}