feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.domain.entity.*
import cn.flightfeather.supervision.domain.enumeration.AuthenticationStatus
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.PinYin
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.AuthService
import cn.flightfeather.supervision.lightshare.vo.AuthSceneIndVo
import cn.flightfeather.supervision.lightshare.vo.AuthSceneRestVo
import cn.flightfeather.supervision.lightshare.vo.AuthSceneVo
import cn.flightfeather.supervision.lightshare.vo.BaseResponse
import com.google.gson.Gson
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import tk.mybatis.mapper.entity.Example
import java.util.*
 
@Service
class AuthServiceImpl(
    private val userinfoMapper: UserinfoMapper,
    private val userInfoWxMapper: UserInfoWxMapper,
    private val companyMapper: CompanyMapper,
    private val personalInfoMapper: PersonalInfoMapper,
    private val baseInfoMapper: BaseInfoMapper,
    private val fumePurifyDeviceMapper: FumePurifyDeviceMapper,
    private val monitorDeviceMapper: MonitorDeviceMapper,
    private val restaurantBaseInfoMapper: RestaurantBaseInfoMapper,
    private val vehicleBaseInfoMapper: VehicleBaseInfoMapper,
    private val industrialBaseInfoMapper: IndustrialBaseInfoMapper,
    private val vocPurifyDeviceMapper: VocPurifyDeviceMapper,
): AuthService {
 
    @Transactional
    override fun authCompany(wxUserId: String, company: Company): BaseResponse<String> {
        val wxUser = userInfoWxMapper.selectByPrimaryKey(wxUserId) ?: return BaseResponse(false, "用户微信id不存在")
        company.ciExtension3 = AuthenticationStatus.YES.des
        if (company.ciGuid == null) {
            company.ciGuid = UUIDGenerator.generate16ShortUUID()
            companyMapper.insert(company)
        } else {
            companyMapper.updateByPrimaryKeySelective(company)
        }
        val bInfo = if (wxUser.uiGuid == null) null else baseInfoMapper.selectByPrimaryKey(wxUser.uiGuid)
        if (bInfo != null && bInfo.ciGuid != company.ciGuid) {
            bInfo.ciGuid = company.ciGuid
            bInfo.ciName = company.ciName
            baseInfoMapper.updateByPrimaryKeySelective(bInfo)
        } else if (bInfo == null) {
            val userInfo = userinfoMapper.selectByPrimaryKey(wxUser.uiGuid)
            val baseInfo = BaseInfo().apply {
                biGuid = wxUser.uiGuid
                biName = userInfo?.realname
                ciGuid = company.ciGuid
                ciName = company.ciName
                biCreateTime = Date()
                biExtension1 = userInfo?.acountname
            }
            baseInfoMapper.insertSelective(baseInfo)
        }
        if (wxUser.ciGuid != company.ciGuid) {
            wxUser.ciGuid = company.ciGuid
            userInfoWxMapper.updateByPrimaryKeySelective(wxUser)
        }
        return BaseResponse(true)
    }
 
    @Transactional
    override fun authScene(wxUserId: String, sceneType: Int, sceneInfo: String): BaseResponse<String> {
//        val user = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false, "用户场景id不存在")
        val wxUser = userInfoWxMapper.selectByPrimaryKey(wxUserId) ?: return BaseResponse(false, "用户微信id不存在")
        val gson = Gson()
        // 所有场景统一的基础信息
        val asVo = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
        var bInfo = if (wxUser.uiGuid == null) null else baseInfoMapper.selectByPrimaryKey(wxUser.uiGuid)
        if (bInfo == null) {
            val cInfo = if (wxUser.ciGuid == null) null else companyMapper.selectByPrimaryKey(wxUser.ciGuid)
            val user = userinfoMapper.selectByPrimaryKey(wxUser.uiGuid)
            if (user == null) {
                val name = getUName(asVo.biName ?: "")
                //新建场景账号及场景信息
                val user = Userinfo().apply {
                    guid = UUIDGenerator.generate16ShortUUID()
                    acountname = name
                    realname = asVo.biName
                    password = "123456"
                    usertypeid = 3
                    usertype = "企业"
                    isenable = true
                    if (asVo.biLocation.isNotEmpty()) extension1 = asVo.biLocation[2]
                    extension2 = sceneType.toString()
                }
                userinfoMapper.insert(user)
            } else {
                user.apply {
                    realname = asVo.biName
                    if (asVo.biLocation.isNotEmpty()) extension1 = asVo.biLocation[2]
                }
                userinfoMapper.updateByPrimaryKeySelective(user)
            }
            bInfo = asVo.toNewBaseInfo(user, cInfo)
            baseInfoMapper.insert(bInfo)
        } else {
            val userInfo = userinfoMapper.selectByPrimaryKey(bInfo.biGuid)
            userInfo?.apply {
                realname = asVo.biName
                if (asVo.biLocation.isNotEmpty()) extension1 = asVo.biLocation[2]
            }
            asVo.updateBaseInfo(bInfo)
            userinfoMapper.updateByPrimaryKeySelective(userInfo)
            baseInfoMapper.updateByPrimaryKeySelective(bInfo)
        }
        val userId = bInfo.biGuid
        when (sceneType) {
            SceneType.Restaurant.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneRestVo::class.java)
                //餐饮店基本信息录入
                var rbInfo = restaurantBaseInfoMapper.selectByPrimaryKey(userId)
                if (rbInfo == null) {
                    rbInfo = info.toNewRestInfo(userId)
                    restaurantBaseInfoMapper.insert(rbInfo)
                } else {
                    info.updateRestInfo(rbInfo)
                    restaurantBaseInfoMapper.updateByPrimaryKeySelective(rbInfo)
                }
                //餐饮店油烟净化装置信息录入
                var fpdInfo = fumePurifyDeviceMapper.selectByExample(Example(FumePurifyDevice::class.java).apply {
                    createCriteria().andEqualTo("fpUserId", userId)
                })?.takeIf { it.isNotEmpty() }?.get(0)
                if (fpdInfo == null) {
                    fpdInfo = info.toNewFpdInfo(bInfo)
                    fumePurifyDeviceMapper.insert(fpdInfo)
                } else {
                    info.updateFpdInfo(fpdInfo)
                    fumePurifyDeviceMapper.updateByPrimaryKeySelective(fpdInfo)
                }
                //餐饮店油烟监测设备信息录入
                var mdInfo = monitorDeviceMapper.selectByExample(Example(MonitorDevice::class.java).apply {
                    createCriteria().andEqualTo("mdUserId", userId)
                })?.takeIf { it.isNotEmpty() }?.get(0)
                if (mdInfo == null) {
                    mdInfo = info.toNewMdInfo(bInfo)
                    monitorDeviceMapper.insert(mdInfo)
                } else {
                    info.updateMdInfo(mdInfo)
                    monitorDeviceMapper.updateByPrimaryKeySelective(mdInfo)
                }
            }
            SceneType.Construction.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
            }
            SceneType.Wharf.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
            }
            SceneType.StorageYard.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
            }
            SceneType.MixingPlant.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
            }
            SceneType.Industrial.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneIndVo::class.java)
                //工业企业基本信息录入
                var rbInfo = industrialBaseInfoMapper.selectByPrimaryKey(userId)
                if (rbInfo == null) {
                    rbInfo = info.toNewIndInfo(userId)
                    industrialBaseInfoMapper.insert(rbInfo)
                } else {
                    info.updateIndInfo(rbInfo)
                    industrialBaseInfoMapper.updateByPrimaryKeySelective(rbInfo)
                }
//                industrialBaseInfoMapper
//                vocPurifyDeviceMapper
            }
            SceneType.VehicleRepair.value -> {
                val info = gson.fromJson(sceneInfo, AuthSceneVo::class.java)
            }
            else-> return BaseResponse(false, "用户场景类型错误")
        }
 
        return BaseResponse(true, "场景认证完成")
    }
 
    override fun authPersonal(wxUserId: String, personalInfo: PersonalInfo): BaseResponse<String> {
        val wxUser = userInfoWxMapper.selectByPrimaryKey(wxUserId) ?: return BaseResponse(false, "用户微信id不存在")
        personalInfo.piExtension3 = AuthenticationStatus.YES.des
        personalInfo.piWxId = wxUserId
        personalInfo.piSceneId = wxUser.uiGuid
        if (personalInfo.piGuid == null) {
            personalInfo.piGuid = UUIDGenerator.generate16ShortUUID()
            personalInfoMapper.insert(personalInfo)
        } else {
            personalInfoMapper.updateByPrimaryKeySelective(personalInfo)
        }
        if (wxUser.piGuid != personalInfo.piGuid) {
            wxUser.piGuid = personalInfo.piGuid
            userInfoWxMapper.updateByPrimaryKeySelective(wxUser)
        }
        return BaseResponse(true)
    }
 
    override fun authStatus(wxUserId: String?, userId: String?): BaseResponse<List<Boolean>> {
        val status = mutableListOf(false, false, false)
        if (wxUserId != null) {
            val wxUser = userInfoWxMapper.selectByPrimaryKey(wxUserId) ?: return BaseResponse(false, "该微信账户不存在")
            //判断企业信息是否认证
            if (wxUser.ciGuid != null) {
                companyMapper.selectByPrimaryKey(wxUser.ciGuid)?.let { c ->
                    if (c.ciExtension3 == AuthenticationStatus.YES.des) status[0] = true
                }
            }
            //判断场景信息是否认证
            if (wxUser.uiGuid != null) {
                baseInfoMapper.selectByPrimaryKey(wxUser.uiGuid)?.let {b ->
                    if (b.biExtension3 == AuthenticationStatus.YES.des) status[1] = true
                }
            }
            //判断个人信息是否认证
            if (wxUser.uiGuid != null) {
                personalInfoMapper.selectByExample(Example(PersonalInfo::class.java).apply {
                    createCriteria().andEqualTo("piSceneId", wxUser.uiGuid)
                        .andEqualTo("piWxId", wxUser.uiOpenId)
                })?.takeIf { it.isNotEmpty() }?.get(0)?.let { p ->
                    if (p.piExtension3 == AuthenticationStatus.YES.des) status[2] = true
                }
            }
        }
        if (userId != null) {
            val user = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false, "该场景账户不存在")
            val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
            //判断企业信息是否认证
            if (baseInfo?.ciGuid != null) {
                companyMapper.selectByPrimaryKey(baseInfo.ciGuid)?.let { c ->
                    if (c.ciExtension3 == AuthenticationStatus.YES.des) status[0] = true
                }
            }
            //判断场景信息是否认证
            if (baseInfo?.biExtension3 == AuthenticationStatus.YES.des) status[1] = true
            //判断个人信息是否认证
            userInfoWxMapper.selectByExample(Example(UserInfoWx::class.java).apply {
                createCriteria().andEqualTo("uiGuid", userId)
            })?.takeIf { it.isNotEmpty() }?.get(0)?.let {
                personalInfoMapper.selectByExample(Example(PersonalInfo::class.java).apply {
                    createCriteria().andEqualTo("piSceneId", it.uiGuid)
                        .andEqualTo("piWxId", it.uiOpenId)
                })?.takeIf { it.isNotEmpty() }?.get(0)?.let { p ->
                    if (p.piExtension3 == AuthenticationStatus.YES.des) status[2] = true
                }
            }
        }
 
        return BaseResponse(true, data = status)
    }
 
    override fun sceneAuthStatus(userId: String?): BaseResponse<List<Boolean>> {
        val status = mutableListOf(false, false, false)
        val base = baseInfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false, "该微信账户不存在")
        //判断企业信息是否认证
        if (base.ciGuid != null) {
            companyMapper.selectByPrimaryKey(base.ciGuid)?.let { c ->
                if (c.ciExtension3 == AuthenticationStatus.YES.des) status[0] = true
            }
        }
        //判断场景信息是否认证
        if (base.biExtension3 == AuthenticationStatus.YES.des) status[1] = true
        //判断个人信息是否认证
        personalInfoMapper.selectByExample(Example(PersonalInfo::class.java).apply {
            createCriteria().andEqualTo("piSceneId", base.biGuid)
        })?.takeIf { it.isNotEmpty() }?.get(0)?.let { p ->
            if (p.piExtension3 == AuthenticationStatus.YES.des) status[2] = true
        }
 
        return BaseResponse(true, data = status)
    }
 
    private fun getUName(sceneName: String): String {
        var uName = if (sceneName.isNotBlank()) PinYin.getPinYinHeader(sceneName) else UUIDGenerator.generateShortUUID()
        var repeated = false
        var i = 1
        do {
            userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
                createCriteria().andEqualTo("acountname", uName)
            }).let {
                repeated = it.isNotEmpty()
                if (repeated) {
                    uName += i
                }
            }
            i++
        } while (repeated && i < 20)
        if (repeated) uName = UUIDGenerator.generateShortUUID()
        return uName
    }
 
    override fun getUnAuthedUsers(): BaseResponse<List<Userinfo?>> {
        val res = userinfoMapper.getUnAuthedUsers()
        return BaseResponse(true, data = res)
    }
}