package cn.flightfeather.supervision.lightshare.service.Impl
|
|
import cn.flightfeather.supervision.domain.entity.*
|
import cn.flightfeather.supervision.domain.enumeration.SceneType
|
import cn.flightfeather.supervision.domain.enumeration.UserType
|
import cn.flightfeather.supervision.domain.mapper.*
|
import cn.flightfeather.supervision.domain.repository.UserInfoRep
|
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
|
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
|
import cn.flightfeather.supervision.domain.repository.UserConfigRep
|
import cn.flightfeather.supervision.lightshare.service.UserinfoService
|
import cn.flightfeather.supervision.lightshare.vo.*
|
import com.github.pagehelper.PageHelper
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook
|
import org.springframework.beans.BeanUtils
|
import org.springframework.stereotype.Service
|
import org.springframework.transaction.annotation.Transactional
|
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 industrialBaseInfoMapper: IndustrialBaseInfoMapper,
|
val userMapMapper: UserMapMapper,
|
val personalInfoMapper: PersonalInfoMapper,
|
val userInfoWxMapper: UserInfoWxMapper,
|
val userInfoRep: UserInfoRep,
|
val userConfigRep: UserConfigRep,
|
) : 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?.get(0) ?: Userinfo()
|
} else {
|
Userinfo()
|
}
|
}
|
|
override fun findOne(id: String): Userinfo {
|
val userInfo = userinfoMapper.selectByPrimaryKey(id)
|
// userInfo?.password = null
|
userMapMapper.selectByPrimaryKey(id)?.let {
|
userInfo?.extension3 = it.svUserId
|
}
|
return userInfo ?: Userinfo()
|
}
|
|
override fun findAll(): MutableList<Userinfo?> = userinfoMapper.selectAll()
|
|
override fun save(userinfo: Userinfo): BaseResponse<Int> {
|
userinfoMapper.selectByPrimaryKey(userinfo.guid)?.run { return BaseResponse(false, "用户已存在", data = 0) }
|
userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
|
createCriteria().andEqualTo("acountname", userinfo.acountname)
|
}).run { if (isNotEmpty()) return BaseResponse(false, "用户登录账户名称已存在", data = 0) }
|
userinfoMapper.selectByExample(Example(Userinfo::class.java).apply {
|
createCriteria().andEqualTo("realname", userinfo.realname)
|
}).run { if (isNotEmpty()) return BaseResponse(false, "用户昵称已存在", data = 0) }
|
|
userinfo.apply {
|
guid = UUIDGenerator.generate16ShortUUID()
|
headIconUrl = ""
|
password = "123456"
|
if (isenable == null) isenable = true
|
uiCreateTime = Date()
|
}
|
val res = userinfoMapper.insert(userinfo)
|
return if (res == 1) {
|
BaseResponse(true, data = res)
|
} else {
|
BaseResponse(false, "插入数据库失败", data = res)
|
}
|
}
|
|
@Transactional
|
override fun save2(info: UserBaseInfo): BaseResponse<Int> {
|
info.userInfo ?: return BaseResponse(false, "用户信息缺失,无法新建用户")
|
info.baseInfo ?: return BaseResponse(false, "用户信息缺失,无法新建用户")
|
val repeatRes = userInfoRep.checkIsRepeat(info.userInfo)
|
if (repeatRes.first) return BaseResponse(false, repeatRes.second)
|
|
info.userInfo.apply {
|
guid = UUIDGenerator.generate16ShortUUID()
|
headIconUrl = ""
|
password = "123456"
|
if (isenable == null) isenable = true
|
uiCreateTime = Date()
|
}
|
info.baseInfo.apply {
|
biGuid = info.userInfo.guid
|
biName = info.userInfo.realname
|
biCreateTime = info.userInfo.uiCreateTime
|
biExtension1 = info.userInfo.acountname
|
}
|
userinfoMapper.insert(info.userInfo)
|
baseInfoMapper.insert(info.baseInfo)
|
return BaseResponse(true)
|
}
|
|
override fun resetPassword(userId: String): BaseResponse<Boolean> {
|
val info = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false, "用户不存在")
|
info.password = "123456"
|
val r = userinfoMapper.updateByPrimaryKeySelective(info)
|
return BaseResponse(r == 1)
|
}
|
|
override fun update(userinfo: Userinfo): BaseResponse<Int> {
|
val res = userinfoMapper.updateByPrimaryKeySelective(userinfo)
|
return if (res == 1) {
|
BaseResponse(true, data = res)
|
} else {
|
BaseResponse(false, "插入数据库失败", data = res)
|
}
|
}
|
|
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()) {
|
val u = result?.get(0)
|
u?.uiLoginTime = Date()
|
userinfoMapper.updateByPrimaryKeySelective(u)
|
userInfoRep.loginLog(u?.guid)
|
userId = u?.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.getByValue(it.extension2?.toInt() ?: -1).des}"
|
}
|
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() == true) {
|
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).apply {
|
condition.userTypeId?.let { andEqualTo("usertypeid", it) }
|
})
|
//todo 2020.8.19 街镇的条件查询需要扩充BaseInfo数据表字段后再实现
|
})
|
|
response.setIntHeader("totalPage", p.pages)
|
response.setIntHeader("currentPage", p.pageNum)
|
response.setIntHeader("totalCount", p.total.toInt())
|
|
return result
|
}
|
|
override fun getBaseInfo(userId: String, wxUserId: String?): UserBaseInfo {
|
val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return UserBaseInfo(userId)
|
userInfo.password = null
|
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
|
SceneType.Industrial.value.toString() -> industrialBaseInfoMapper
|
else -> null
|
}
|
|
val specialInfo = mapper?.selectByPrimaryKey(baseInfo?.biGuid)
|
val companyInfo =
|
companyMapper.selectByPrimaryKey(if (baseInfo?.ciGuid != null) baseInfo.ciGuid else wxUser?.ciGuid)
|
val personalInfo = personalInfoMapper.selectByExample(Example(PersonalInfo::class.java).apply {
|
createCriteria().andEqualTo("piSceneId", userId).andEqualTo("piWxId", wxUser?.uiOpenId)
|
}).takeIf { it.isNotEmpty() }?.get(0)
|
|
return UserBaseInfo(userId, userInfo.realname, userInfo, 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)
|
}
|
|
override fun getSceneCount(userId: String, condition: UserSearchCondition): BaseResponse<Triple<Int, Int, Int>> {
|
// val user = userinfoMapper.selectByPrimaryKey(userId)
|
// val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
|
// condition.districtName = condition.districtName ?: user?.extension1
|
|
val config = userConfigRep.getUserConfigBySubType(userId)
|
val c = UserSearchCondition.fromUserConfig(config, condition).apply {
|
userTypeId = UserType.Enterprise.value.toByte()
|
// sceneTypes = condition.sceneTypes
|
}
|
|
val result = userinfoMapper.searchUser(c)
|
val total = result.size
|
val enabled = result.filter { u ->
|
return@filter u.isenable == true
|
}.size
|
val disabled = total - enabled
|
|
return BaseResponse(true, data = Triple(total, enabled, disabled))
|
}
|
|
override fun createUsers(workbook: HSSFWorkbook) {
|
TODO("Not yet implemented")
|
}
|
}
|