feiyu02
2024-11-19 752e00503f672ddfe2066afb6c235721a3a912b5
src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/UserinfoServiceImpl.kt
@@ -1,21 +1,20 @@
package cn.flightfeather.supervision.lightshare.service.Impl
import cn.flightfeather.supervision.common.net.WXHttpService
import cn.flightfeather.supervision.domain.entity.BaseInfo
import cn.flightfeather.supervision.domain.entity.Company
import cn.flightfeather.supervision.domain.entity.UserInfoWx
import cn.flightfeather.supervision.domain.entity.Userinfo
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.alibaba.fastjson.JSONObject
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
@@ -29,7 +28,12 @@
    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条件查询
@@ -39,25 +43,87 @@
        criteria.andEqualTo("acountname", userinfo.acountname)
        val result = userinfoMapper.selectByExample(example)
        return if (result.isNotEmpty()) {
            result[0]
            result?.get(0) ?: Userinfo()
        } else {
            Userinfo()
        }
    }
    override fun findOne(id: String): 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 findAll(): MutableList<Userinfo?> = userinfoMapper.selectAll()
    override fun save(userinfo: Userinfo): Int = userinfoMapper.insert(userinfo)
    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) }
    override fun update(userinfo: Userinfo): Int = userinfoMapper.updateByPrimaryKeySelective(userinfo)
        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)
@@ -66,11 +132,15 @@
        val example = Example(Userinfo::class.java)
        val criteria = example.createCriteria()
        criteria.andEqualTo("acountname", loginRequestVo.userName)
                .andEqualTo("password",loginRequestVo.password)
            .andEqualTo("password", loginRequestVo.password)
        val result = userinfoMapper.selectByExample(example)
        return AccessToken().apply {
            if (result.isNotEmpty()) {
                userId = result[0].guid
                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
@@ -161,24 +231,24 @@
            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)}"
            .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)
                }
            }
            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
    }
@@ -206,29 +276,38 @@
        }
    }
    override fun changePassword(userId: String, oldPassword: String, newPassword: String): Int {
    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) {
            0
            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> {
    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 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()) {
            if (condition.searchText?.isNotBlank() == true) {
                and(createCriteria()
                        .orLike("acountname", "%${condition.searchText}%")
                        .orLike("realname", "%${condition.searchText}%")
                    .orLike("acountname", "%${condition.searchText}%")
                    .orLike("realname", "%${condition.searchText}%")
                )
            }
            if (condition.sceneTypes.isNotEmpty()) {
@@ -239,33 +318,44 @@
                })
            }
            and(createCriteria().andEqualTo("extension1", districtName)
                    .andEqualTo("isenable", true))
                .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): UserBaseInfo {
    override fun getBaseInfo(userId: String, wxUserId: String?): UserBaseInfo {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return UserBaseInfo(userId)
        val baseInfo = baseInfoMapper.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
            else -> restaurantBaseInfoMapper
            SceneType.Industrial.value.toString() -> industrialBaseInfoMapper
            else -> null
        }
        val specialInfo = mapper.selectByPrimaryKey(baseInfo.biGuid)
        val companyInfo = companyMapper.selectByPrimaryKey(baseInfo.ciGuid)
        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, baseInfo, companyInfo, specialInfo)
        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>> {
    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) }
@@ -276,4 +366,29 @@
        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")
    }
}