feiyu02
2022-10-21 f22c4b9230808fed4fec80c435eccb4c833349a0
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.domain.entity.BaseInfo
import cn.flightfeather.supervision.domain.entity.Company
import cn.flightfeather.supervision.domain.entity.Userinfo
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.UserinfoService
import cn.flightfeather.supervision.lightshare.vo.*
import com.github.pagehelper.PageHelper
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.math.BigDecimal
import java.util.*
import javax.servlet.http.HttpServletResponse
 
@Service
class UserinfoServiceImpl(
    val userinfoMapper: UserinfoMapper,
    val baseInfoMapper: BaseInfoMapper,
    val companyMapper: CompanyMapper,
    val restaurantBaseInfoMapper: RestaurantBaseInfoMapper,
    val vehicleBaseInfoMapper: VehicleBaseInfoMapper,
    val userMapMapper: UserMapMapper,
    val personalInfoMapper: PersonalInfoMapper,
    val userInfoWxMapper: UserInfoWxMapper
) : UserinfoService {
 
    //根据userinfo条件查询
    override fun findOneByName(userinfo: Userinfo): Userinfo {
        val example = Example(Userinfo::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("acountname", userinfo.acountname)
        val result = userinfoMapper.selectByExample(example)
        return if (result.isNotEmpty()) {
            result[0]
        } else {
            Userinfo()
        }
    }
 
    override fun findOne(id: String): Userinfo {
        val userInfo = userinfoMapper.selectByPrimaryKey(id)
        userMapMapper.selectByPrimaryKey(id)?.let {
            userInfo?.extension3 = it.svUserId
        }
        return userInfo ?: Userinfo()
    }
 
    override fun findAll(): MutableList<Userinfo> = userinfoMapper.selectAll()
 
    override fun save(userinfo: Userinfo): Int = userinfoMapper.insert(userinfo)
 
    override fun update(userinfo: Userinfo): Int = userinfoMapper.updateByPrimaryKeySelective(userinfo)
 
    override fun delete(id: String): Int = userinfoMapper.deleteByPrimaryKey(id)
 
    override fun login(loginRequestVo: LoginRequestVo): AccessToken {
        if (loginRequestVo.userName.isNullOrEmpty() || loginRequestVo.password.isNullOrEmpty()) return AccessToken()
        val example = Example(Userinfo::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("acountname", loginRequestVo.userName)
                .andEqualTo("password",loginRequestVo.password)
        val result = userinfoMapper.selectByExample(example)
        return AccessToken().apply {
            if (result.isNotEmpty()) {
                userId = result[0].guid
                val sUser = userMapMapper.selectByPrimaryKey(userId)
                sUserId = sUser?.svUserId
                success = true
            } else {
                success = false
            }
        }
    }
 
    override fun register(loginRequestVo: LoginRequestVo): AccessToken {
        val example = Example(Userinfo::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("acountname", loginRequestVo.userName)
        val result = userinfoMapper.selectByExample(example)
        if (result.isNotEmpty()) {
            return AccessToken().apply { success = false }
        } else {
            val userInfo = Userinfo().apply {
                guid = UUIDGenerator.generate16ShortUUID()
                acountname = loginRequestVo.userName
                realname = loginRequestVo.userName
                password = loginRequestVo.password
                usertypeid = 3
                usertype = "企业"
                isenable = true
            }
            save(userInfo)
            return AccessToken().apply {
                userId = userInfo.guid
                success = true
            }
        }
    }
 
    override fun register2(loginRequestVo: LoginRequestVo): AccessToken {
        val result = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
            createCriteria().andEqualTo("realname", loginRequestVo.userName)
        })
        if (result.isNotEmpty()) {
            return AccessToken().apply { success = false }
        } else {
            val userInfo = Userinfo().apply {
                guid = UUIDGenerator.generate16ShortUUID()
                acountname = loginRequestVo.userName
                realname = loginRequestVo.userName
                password = loginRequestVo.password
                usertypeid = 3
                usertype = "企业"
                isenable = true
                extension2 = loginRequestVo.sceneType
            }
            val company = Company().apply {
                ciGuid = UUIDGenerator.generate16ShortUUID()
                ciName = loginRequestVo.department
                ciAddress = loginRequestVo.address
                ciLongitude = BigDecimal.ZERO
                ciLatitude = BigDecimal.ZERO
                ciOrgCode = loginRequestVo.orgCode
                ciBuildDate = Date()
                ciTelephone = loginRequestVo.telephone
            }
            val baseInfo = BaseInfo().apply {
                biGuid = userInfo.guid
                biName = userInfo.realname
                ciGuid = company.ciGuid
                ciName = company.ciName
                biTelephone = loginRequestVo.telephone
                biAddress
                biCreateTime = Date()
                biUpdateTime = Date()
                biExtension1 = userInfo.acountname
            }
            save(userInfo)
            baseInfoMapper.insert(baseInfo)
            companyMapper.insert(company)
 
            return AccessToken().apply {
                userId = userInfo.guid
                success = true
            }
        }
    }
 
    override fun getAddressBook(userId: String): List<FriendVo> {
        val resultList = mutableListOf<FriendVo>()
        val userInfo = findOne(userId)
        userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
            createCriteria().andEqualTo("extension1", userInfo.extension1)
                .andGreaterThanOrEqualTo("usertypeid", userInfo.usertypeid)
        })
        .forEach {
            val groupName = if (it.extension1.isNullOrEmpty()){
                "未分组"
            } else {
                "${it.extension1!!}-${it.usertype}-${SceneType.getNameByValue(it.extension2?.toInt() ?: -1)}"
            }
            val friend = if (it.guid != userId) {
                FriendVo(groupName)
            } else {
                FriendVo("我")
            }
            BeanUtils.copyProperties(it, friend)
            if (it.guid == userId) {
                resultList.add(0, friend)
            } else {
                resultList.add(friend)
            }
        }
        return resultList
    }
 
    override fun upLoadAccountPic(userId: String, files: Array<MultipartFile>): String {
        if (files.isNotEmpty()) {
            val file = files[0]
            val fileName = files[0].originalFilename
            val basePath = "D:/02product/05ledger/images/"
            val path = "accounts/$userId/"
            try {
                //调用文件保存方法
                FileUtil.uploadFile(file.bytes, basePath + path, fileName!!)
            } catch (e: Exception) {
                e.printStackTrace()
            }
 
            val user = Userinfo().apply {
                guid = userId
                headIconUrl = "$path$fileName"
            }
            userinfoMapper.updateByPrimaryKeySelective(user)
            return "$path$fileName"
        } else {
            return ""
        }
    }
 
    override fun changePassword(userId: String, oldPassword: String, newPassword: String): BaseResponse<String> {
        if (newPassword.trim() == "") return BaseResponse(false, "新密码不能为空")
        if (oldPassword == newPassword) return BaseResponse(false, "新密码不能和原密码相同")
 
        val userInfo = findOne(userId)
        return if (oldPassword != userInfo.password) {
            BaseResponse(false, "原密码错误")
        } else {
            val newUserInfo = Userinfo().apply {
                guid = userInfo.guid
                password = newPassword
                remark = "pwChanged"
            }
            update(newUserInfo)
            BaseResponse(true, "密码修改成功")
        }
    }
 
    override fun searchUser(userId: String, condition: UserSearchCondition, page: Int, perPage: Int, response: HttpServletResponse): List<Userinfo> {
        val user = userinfoMapper.selectByPrimaryKey(userId)
        val districtName = condition.districtName ?: user.extension1
 
        val p = PageHelper.startPage<Userinfo>(page, perPage)
        val result = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
            if (condition.searchText.isNotBlank()) {
                and(createCriteria()
                        .orLike("acountname", "%${condition.searchText}%")
                        .orLike("realname", "%${condition.searchText}%")
                )
            }
            if (condition.sceneTypes.isNotEmpty()) {
                and(createCriteria().apply {
                    condition.sceneTypes.forEach {
                        orEqualTo("extension2", it)
                    }
                })
            }
            and(createCriteria().andEqualTo("extension1", districtName)
                    .andEqualTo("isenable", true))
            //todo 2020.8.19 街镇的条件查询需要扩充BaseInfo数据表字段后再实现
        })
 
        response.setIntHeader("totalPage", p.pages)
        response.setIntHeader("currentPage", p.pageNum)
 
        return result
    }
 
    override fun getBaseInfo(userId: String, wxUserId: String?): UserBaseInfo {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return UserBaseInfo(userId)
        val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
        val wxUser = if (wxUserId != null) userInfoWxMapper.selectByPrimaryKey(wxUserId) else null
 
        val mapper = when (userInfo.extension2) {
            SceneType.Restaurant.value.toString() -> restaurantBaseInfoMapper
            SceneType.VehicleRepair.value.toString() -> vehicleBaseInfoMapper
            else -> null
        }
 
        val specialInfo = mapper?.selectByPrimaryKey(baseInfo?.biGuid)
        val companyInfo = companyMapper.selectByPrimaryKey(if (baseInfo?.ciGuid != null) baseInfo.ciGuid else wxUser?.ciGuid)
        val personalInfo = personalInfoMapper.selectByPrimaryKey(wxUser?.piGuid)
 
        return UserBaseInfo(userId, userInfo.realname, baseInfo, companyInfo, specialInfo, personalInfo)
    }
 
    override fun search(district: String?, sceneType: Int?, userType: Int?, page: Int, perPage: Int): BaseResponse<List<Userinfo>> {
        val result = userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
            createCriteria().apply {
                district?.let { andEqualTo("extension1", it) }
                sceneType?.let { andEqualTo("extension2", it) }
                userType?.let { andEqualTo("usertypeid", it) }
            }
        })
 
        return BaseResponse(true, data = result)
    }
}