package com.ozs.controller.upload;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.common.utils.MinioUtils;
import com.ozs.common.utils.StringUtils;
import com.ozs.common.utils.uuid.IdUtils;
import com.ozs.entity.BaseRailwayManagement;
import com.ozs.service.BaseCameraManagementService;
import com.ozs.service.BaseRailwayManagementService;
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.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.HashMap;
import java.util.Map;

@RestController
public class UploadController {
    public static final String PATTERN = "^[0-9]*$";
    @Value("${minio.bucketName}")
    private String bucketName;

    @Resource
    MinioUtils minioUtils;
    @Autowired
    BaseCameraManagementService baseCameraManagementService;
    @Value("${base.imgUrl}")
    private String imgUrl;
    @Autowired
    private BaseRailwayManagementService baseRailwayManagementService;


    @PostMapping("/api/putFile")
    public JSONObject upLoadImage(@RequestParam("stream") MultipartFile stream,
                                  @RequestParam(value = "size", required = false) Long size,
                                  @RequestParam(value = "contentType", required = false) String contentType,
                                  @RequestParam(value = "alarmMile", required = false) Integer alarmMile,
                                  @RequestParam(value = "alarmRailway", required = false) String alarmRailway,
                                  @RequestParam(value = "lineDir", required = false) Integer lineDir) throws Exception {

        JSONObject jsonObject = new JSONObject();
        if (stream.isEmpty()) {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "不能上传空文件哦");
            return jsonObject;
        }
        if (lineDir!=1&&lineDir!=2){
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "行别填写错误");
            return jsonObject;
        }
        if (!alarmMile.toString().matches(PATTERN)) {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "报警里程位置填写格式不正确");
            return jsonObject;
        }
        if (!size.toString().matches(PATTERN)) {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "要上传的stream的填写格式不正确");
            return jsonObject;
        }
        if (ObjectUtils.isEmpty(contentType)){
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "Content Type不能为空");
            return jsonObject;
        }
        LambdaQueryWrapper<BaseRailwayManagement> queryWrapper = new LambdaQueryWrapper<BaseRailwayManagement>();
        if (!ObjectUtils.isEmpty(alarmRailway)) {
            queryWrapper.eq(BaseRailwayManagement::getRailwayCode, alarmRailway);
        }
        BaseRailwayManagement baseRailwayManagement = baseRailwayManagementService.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(baseRailwayManagement)){
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "填写的报警线路编码不存在");
            return jsonObject;
        }
        //图片保存路径
        //String fileUploadPath ="/"+userId+"/image";
        String uploadFile = null;
        if (!stream.isEmpty()) {

            String imageName = stream.getOriginalFilename();
            if (StringUtils.isNotBlank(imageName)) {
                String filename = IdUtils.fastSimpleUUID() + stream.getOriginalFilename().substring(stream.getOriginalFilename().lastIndexOf("."));
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
                String format1 = dateFormat1.format(new Date());
                String imgName = alarmRailway + "/" + lineDir + "/" + alarmMile + "/" + format1 + "/" + filename;
                minioUtils.minIoClientUpload(stream.getInputStream(), imgName);
                uploadFile = imgUrl+"/" + alarmRailway + "/" + lineDir + "/" + alarmMile + "/" + format1 + "/" + filename;
            }
        } else {
            jsonObject.put("resultCode", 0);
            jsonObject.put("message", "失败");
            jsonObject.put("data", "图片为null");
            return jsonObject;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("url", uploadFile);
        jsonObject.put("resultCode", 1);
        jsonObject.put("message", "成功");
        jsonObject.put("data", map);
        return jsonObject;
    }
}
