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 com.google.gson.Gson
|
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.time.LocalDate
|
import java.time.format.DateTimeFormatter
|
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: String, sign: MultipartFile?, seal: MultipartFile?): BaseResponse<List<String>> {
|
sign ?: return BaseResponse(false, "签名图片未上传成功")
|
val infoVo = Gson().fromJson(info, CommitmentVo::class.java)
|
|
val signUrl = infoVo.sign.ifBlank { 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"
|
}
|
}
|
// val picName = contractName.replace("pdf", "jpg")
|
|
//补全信息
|
infoVo.apply {
|
if (city.isBlank()) city = "上海市"
|
if (district.isBlank()) district = userInfo.extension1 ?: ""
|
if (year.isBlank()) {
|
val t = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")).split("-")
|
year = t[0]
|
month = t[1]
|
day = t[2]
|
}
|
}
|
params.apply {
|
put("City", infoVo.city)
|
put("District", infoVo.district)
|
put("Department", infoVo.department)
|
put("SocialCode", infoVo.socialCode)
|
put("Number", infoVo.number)
|
put("JuridicalPerson", infoVo.juridicalPerson)
|
put("IdNo", infoVo.idNo)
|
put("Type1", if (infoVo.type1) "√" else "□")
|
put("Type2", if (infoVo.type2) "√" else "□")
|
put("Type3", if (infoVo.type3) "√" else "□")
|
put("Type4", if (infoVo.type4) "√" else "□")
|
put("Sign", signUrl)
|
put("Seal", sealUrl)
|
put("Year", infoVo.year)
|
put("Month", infoVo.month)
|
put("Day", infoVo.day)
|
}
|
val param = DynamicParam(templatePath, templateName, contractPath, contractName, params)
|
val picPaths = GeneratePdfUtil.generateContract(param)
|
var picPath = ""
|
for (i in picPaths.indices) {
|
val picName = contractName.replace(".pdf", "(${i}).jpg");
|
if (picPath.isNotBlank()) {
|
picPath += ";"
|
}
|
picPath += (path + picName)
|
}
|
|
//将生成的承诺书记录插入数据库
|
val commitment = Commitment().apply {
|
cmGuid = UUIDGenerator.generate16ShortUUID()
|
uiGuid = userId
|
cmUrl = picPath
|
cmPdfUrl = path + contractName
|
cmCreateTime = Date()
|
}
|
val r = commitmentMapper.insert(commitment)
|
|
return BaseResponse(r == 1, data = listOf(commitment.cmUrl, commitment.cmPdfUrl))
|
}
|
}
|