riku
2021-09-24 4f1b7973c53b45f57e451191bfd5a3d2136a004c
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.domain.entity.Commitment
import cn.flightfeather.supervision.domain.mapper.CommitmentMapper
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.infrastructure.utils.FileUtil
import cn.flightfeather.supervision.infrastructure.utils.UUIDGenerator
import cn.flightfeather.supervision.lightshare.service.CommitmentService
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.servlet.http.HttpServletResponse
 
/**
 * @author riku
 * Date: 2020/9/27
 */
@Service
class CommitmentServiceImpl(
        val commitmentMapper: CommitmentMapper
) : CommitmentService {
 
    override fun getLetterOfCommitment(userId: String, page: Int, perPage: Int, response: HttpServletResponse): List<Commitment> {
        val p = PageHelper.startPage<Commitment>(page, perPage)
        val result = commitmentMapper.selectByExample(Example(Commitment::class.java).apply {
            createCriteria().andEqualTo("uiGuid", userId)
            orderBy("cmCreateTime").desc()
        })
        response.setIntHeader("totalPage", p.pages)
        response.setIntHeader("currentPage", p.pageNum)
        return result
    }
 
    override fun uploadLetterOfCommitment(userId: String, commitmentVoList: String, files: Array<MultipartFile>): Boolean {
        val mapper = ObjectMapper()
 
        val commitmentVos = mapper.readValue<List<Commitment>>(commitmentVoList)
 
        var success = 0
        commitmentVos.forEach {
            var picPath = ""
            val time = DateUtil().DateToString(Date(), DateUtil.DateStyle.YYYY_MM)
            files.forEach { file ->
                val fileName = file.originalFilename
                //TODO 此处的文件路径需要修改为动态配置
                val basePath = "D:/02product/05ledger/images/"
                val path = "commitments/$time/$userId/"
                picPath += if (picPath.isEmpty()) {
                    "$path$fileName"
                } else {
                    ";$path$fileName"
                }
                try {
                    //调用文件保存方法
                    FileUtil().uploadFile(file.bytes, basePath + path, fileName!!)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
 
            val commitment = Commitment().apply {
                cmGuid = UUIDGenerator.generate16ShortUUID()
                uiGuid = userId
                cmUrl = picPath
                cmCreateTime = Date()
            }
            val r = commitmentMapper.insert(commitment)
            if (r == 1) {
                success++
            }
        }
 
        return commitmentVos.size == success
    }
}