package com.ozs.web.controller.upload;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.utils.MinioUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.uuid.IdUtils;
import com.ozs.service.entity.MsgAlarm;
import com.ozs.service.service.MsgAlarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/system/upLoad")
public class UploadController {

    @Value("${minio.bucketName}")
    private String bucketName;

    @Resource
    MinioUtils minioUtils;
    @Autowired
    MsgAlarmService msgAlarmService;


    @PostMapping("/upLoadImage")
    public AjaxResult upLoadImage(@RequestParam("file") MultipartFile image,
                                  @RequestParam(value = "cameraCode", required = false) String cameraCode,
                                  @RequestParam(value = "lineType", required = false) String lineType) throws Exception {
        if (image.isEmpty()) {
            return AjaxResult.error("不能上传空文件哦");
        }
        //图片保存路径
        //String fileUploadPath ="/"+userId+"/image";
        String uploadFile = null;
        if (image != null && !image.isEmpty()) {

            String imageName = image.getOriginalFilename();
            if (StringUtils.isNotBlank(imageName)) {
                LambdaQueryWrapper<MsgAlarm> lw = new LambdaQueryWrapper<MsgAlarm>();
                if (!ObjectUtils.isEmpty(cameraCode)) {
                    lw.eq(MsgAlarm::getAlarmCamera, cameraCode);
                    lw.orderByDesc(MsgAlarm::getAlarmTime);
                }
                List<MsgAlarm> list = msgAlarmService.list(lw);
                MsgAlarm msgAlarm = list.get(0);
                String date = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
                String filename = IdUtils.fastSimpleUUID() + image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf("."));
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
                String format1 = dateFormat1.format(new Date(msgAlarm.getAlarmTime()));
                String imgName = msgAlarm.getAlarmLine() + "/" + lineType + "/" + msgAlarm.getAlarmMile() + format1 + "/" + filename;
                minioUtils.minIoClientUpload(image.getInputStream(), imgName);
                uploadFile = "/" + msgAlarm.getAlarmLine() + "/" + lineType + "/" + msgAlarm.getAlarmMile() + "/" + format1 + "/" + filename;

            }
        } else {
            return AjaxResult.error("上传功能出错");
        }
        return AjaxResult.success(uploadFile);
    }
}
