From 237d7c42498806a3ca205f63d151671a45304854 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期四, 28 七月 2022 09:19:11 +0800
Subject: [PATCH] 1. 承诺书pdf生产模块

---
 src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/CommitmentServiceImpl.kt |   87 ++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 85 insertions(+), 2 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/CommitmentServiceImpl.kt b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/CommitmentServiceImpl.kt
index 85e8d33..d854dbc 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/CommitmentServiceImpl.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/lightshare/service/Impl/CommitmentServiceImpl.kt
@@ -1,18 +1,32 @@
 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
 
 /**
@@ -21,8 +35,11 @@
  */
 @Service
 class CommitmentServiceImpl(
-        val commitmentMapper: CommitmentMapper
+        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)
@@ -56,7 +73,7 @@
                 }
                 try {
                     //璋冪敤鏂囦欢淇濆瓨鏂规硶
-                    FileUtil().uploadFile(file.bytes, basePath + path, fileName!!)
+                    FileUtil.uploadFile(file.bytes, basePath + path, fileName!!)
                 } catch (e: Exception) {
                     e.printStackTrace()
                 }
@@ -76,4 +93,70 @@
 
         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)
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3