riku
2025-07-02 3013b813e5df6977c0be921928f73b1a3adde290
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
package cn.flightfeather.thirdappmodule.repository
 
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.common.net.ResultObserver
import cn.flightfeather.thirdappmodule.common.net.RetrofitFactory
import cn.flightfeather.thirdappmodule.httpservice.CommonService
import cn.flightfeather.thirdappmodule.httpservice.DomainItemService
import cn.flightfeather.thirdappmodule.model.enumreation.MediaFileType
import cn.flightfeather.thirdappmodule.repository.dao.MediaTypeAliasDao
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.Response
import java.io.File
 
/**
 * @author riku
 * Date: 2020/4/23
 */
class CommonRepository {
    companion object {
        @JvmStatic
        val instance: CommonRepository by lazy(LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository()}
    }
    private val mediaTypeAliasDao = MediaTypeAliasDao()
    val retrofit = RetrofitFactory.instance.retrofit
 
    fun upLoadCrashInfo(accountName: String, file: File, resultCallBack: ResultCallBack<Boolean>) {
        val builder = MultipartBody.Builder()
        val requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file)
        builder.addFormDataPart("files", file.name, requestBody)
        builder.setType(MultipartBody.FORM)
        val multipartBody = builder.build()
        val partList = mutableListOf<MultipartBody.Part>()
        partList.addAll(multipartBody.parts())
 
        val service = RetrofitFactory.instance.retrofit.create(CommonService::class.java)
            .upLoadCrashInfo(accountName, partList)
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.trampoline())
            .subscribe({
                resultCallBack.onSuccess(it.body())
            }, {
                resultCallBack.onFailure()
            })
    }
 
 
    /**
     * 获取“任意拍”模块展示的图片类型
     * @date 2025.7.1 修改为从服务端远程获取,并修改返回类型
     */
    fun getMediaFileTypes(sceneTypeId: Int, resultCallBack: ResultCallBack<List<MediaFileType>>) {
//        resultCallBack.onSuccess(MediaFileType.getList(sceneTypeId))
 
        val service = retrofit.create(DomainItemService::class.java).getMediaFileType(sceneTypeId)
 
        RetrofitFactory.executeResult(service, object : ResultObserver<Map<String?, String?>>() {
            override fun onSuccess(result: Map<String?, String?>?) {
                val res = mutableListOf<MediaFileType>()
                result?.forEach { (t, u) ->
                    if (t == null || u == null) return@forEach
                    res.add(MediaFileType(t.toInt(), u))
                }
                resultCallBack.onSuccess(res)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    /**
     * 获取媒体文件类型的别名
     * @param sceneTypeId 场景id
     * @param mediaFileType 媒体文件类型id
     */
    fun getAlias(sceneTypeId: Int, mediaFileType: Int, resultCallBack: ResultCallBack<String>) {
        val dbService = mediaTypeAliasDao.getAlias(sceneTypeId, mediaFileType)
                .map { Response.success(it) }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<String>() {
            override fun onSuccess(result: String?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    /**
     * 更新媒体文件类型的别名
     * @param sceneTypeId 场景id
     * @param mediaFileType 媒体文件类型id
     * @param alias 别名
     */
    fun updateAlias(sceneTypeId: Int, mediaFileType: MediaFileType, alias: String, resultCallBack: ResultCallBack<Boolean>) {
        val dbService = mediaTypeAliasDao.updateAlias(sceneTypeId, mediaFileType, alias)
                .map { Response.success(it) }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<Boolean>() {
            override fun onSuccess(result: Boolean?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
}