feiyu02
2022-07-28 237d7c42498806a3ca205f63d151671a45304854
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
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.pdf.DynamicParam
import cn.flightfeather.supervision.common.pdf.GeneratePdfUtil
import cn.flightfeather.supervision.common.pdf.PdfUtil
import cn.flightfeather.supervision.domain.entity.Commitment
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.enumeration.UserType
import cn.flightfeather.supervision.domain.mapper.CommitmentMapper
import cn.flightfeather.supervision.domain.mapper.UserinfoMapper
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 cn.flightfeather.supervision.lightshare.vo.BaseResponse
import cn.flightfeather.supervision.lightshare.vo.CommitmentVo
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.github.pagehelper.PageHelper
import net.coobird.thumbnailator.tasks.io.InputStreamImageSource
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import tk.mybatis.mapper.entity.Example
import java.awt.Image
import java.io.File
import java.util.*
import javax.imageio.ImageReader
import javax.imageio.stream.ImageInputStream
import javax.servlet.http.HttpServletResponse
 
/**
 * @author riku
 * Date: 2020/9/27
 */
@Service
class CommitmentServiceImpl(
        val commitmentMapper: CommitmentMapper,val userinfoMapper: UserinfoMapper
) : CommitmentService {
 
    @Value("\${imgPath}")
    lateinit var imgPath: String
 
    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
    }
 
    override fun createLetterOfCommitment(userId: String, info: CommitmentVo, sign: MultipartFile, seal: MultipartFile?): BaseResponse<String> {
        val signUrl = FileUtil.compressImage2(sign.bytes, 400)
        val sealUrl = if (seal != null) FileUtil.compressImage2(seal.bytes, 400) else ""
 
        val templatePath = (Thread.currentThread().contextClassLoader?.getResource("/")?.path ?: "src/main") + "/resources/templates/"
 
        val time = DateUtil().DateToString(Date(), DateUtil.DateStyle.YYYY_MM)
        val path = "commitments/$time/$userId/"
        val contractPath = imgPath + path
 
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return BaseResponse(false)
 
        var templateName = ""
        var contractName = ""
        val params = mutableMapOf<String, Any>()
        val now = Date()
        when (userInfo.extension2?.toIntOrNull()) {
            SceneType.Restaurant.value -> {
                templateName = "commitment-restaurant.ftl"
                contractName = "commitment-restaurant-${now.time}.pdf"
            }
            SceneType.Construction.value,
            SceneType.Wharf.value,
            SceneType.StorageYard.value,
            SceneType.MixingPlant.value,
            SceneType.Industrial.value -> {
                templateName = "commitment-construction.ftl"
                contractName = "commitment-construction-${now.time}.pdf"
            }
            SceneType.VehicleRepair.value -> {
                templateName = "commitment-vehicle.ftl"
                contractName = "commitment-vehicle-${now.time}.pdf"
            }
        }
        params.apply {
            put("City", info.city)
            put("District", info.district)
            put("Department", info.department)
            put("SocialCode", info.socialCode)
            put("Number", info.number)
            put("JuridicalPerson", info.juridicalPerson)
            put("IdNo", info.idNo)
            put("Type1", if (info.type1) "✓" else "□")
            put("Type2", if (info.type2) "✓" else "□")
            put("Type3", if (info.type3) "✓" else "□")
            put("Type4", if (info.type4) "✓" else "□")
            put("Sign", signUrl)
            put("Seal", sealUrl)
            put("Year", info.year)
            put("Month", info.month)
            put("Day", info.day)
        }
        val param = DynamicParam(templatePath, templateName, contractPath, contractName, params)
        GeneratePdfUtil.generateContract(param)
        //将生产的承诺书记录插入数据库
        val commitment = Commitment().apply {
            cmGuid = UUIDGenerator.generate16ShortUUID()
            uiGuid = userId
            cmUrl = path + contractName
            cmCreateTime = Date()
        }
        val r = commitmentMapper.insert(commitment)
 
        return BaseResponse(r == 1, data = path)
    }
}