package com.persagy.server.labsl.controller

import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.server.labsl.models.entities.pic.Picture
import com.persagy.server.labsl.services.pic.PictureService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import lombok.extern.slf4j.Slf4j
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*
import org.springframework.web.multipart.MultipartFile
import java.util.*

/**
 * 图片接口
 *
 * @author wx
 */
@Api(tags = ["006、图片接口"])
@RestController
@RequestMapping("/picture")
@Slf4j
class PictureController {

    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(PictureController::class.java)
    } // Companion object

    /**
     * 上传图片
     *
     * @param   file         图片文件
     * @param   uid          图片id
     * @return              创建结果信息
     */
    @ApiOperation(value = "上传图片", notes = "")
    @PostMapping(value = ["/create"])
    fun create(@RequestParam(value = "file", required = true) file: MultipartFile, @RequestParam(required = false) uid: String?): SCreateResponse<String> {
        var response = SCreateResponse<String>()
        try {
            val picture = Picture()
            if(uid.isNullOrEmpty()){
                picture.id = UUID.randomUUID().toString().replace("-","")
            }else{
                PictureService.delete(SFilter.eq("id",uid!!))
                picture.id = uid
            }
            picture.pic = file.bytes
            logger.debug("name："+file.name)
            logger.debug("长度："+file.size)
//            PictureService.insert(picture)
            response.result = SResponseType.success
            response.entityList = arrayListOf(picture.id!!)
        } catch (e: Exception) {
            response.result = SResponseType.failure
            response.message = e.message.toString()
            e.printStackTrace()
        }
        return response
    } // Function create()

    /**
     * 根据id删除图片
     *
     * @param   idList      id数组
     * @return              删除的结果信息
     */
    @ApiOperation(value = "根据id删除图片信息", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Picture>): SBaseResponse {
        return PictureService.deleteByKeysList(idList)
    } // Function delete()

    /**
     * 查询图片信息
     *
     * @param   Id             查询条件id
     * @return                 查询结果
     */
    @ApiOperation(value = "查询图片信息", notes = "")
    @GetMapping(value = ["/query"])
    fun query(@RequestParam id: String) {
        val picture = PictureService.select(SFilter.eq("id", id)).entity()

            logger.debug(picture?.toJson())

    } // Function query()

} //