package com.chushang.inspection.work.controller;

import cn.hutool.core.lang.Assert;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.inspection.work.dto.ImageDTO;
import com.chushang.inspection.work.service.WrkImgService;
import lombok.extern.java.Log;
import org.aspectj.weaver.loadtime.Aj;
import org.hibernate.validator.constraints.Range;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

/**
 * 工单图片(wrk_img)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/wrk/img")
public class WrkImgController {

    @Resource
    private WrkImgService wrkImgService;

    /**
     * 工单图片上传
     * @param file 文件
     * @param imgType 文件类型
     * @param wrkId 工单id
     * @param locationAddress 定位地址
     */
    @Validated
    @SysLog("图片上传")
    @PostMapping(value = "/picture")
    public AjaxResult uploadPicture(@RequestParam("file") MultipartFile file,
                                    @NotNull(message = "图片类型不能为空")
                                    @Range(min = 1, max = 30, message = "非法的图片类型")
                                    Integer imgType,
                                    @NotNull(message = "工单id不能为空")
                                    Long wrkId,
                                    @Size(max = 255, message = "定位地址不能超过255个字符")
                                    String locationAddress) {
        Assert.isFalse(file.isEmpty(), "【{}】是空的", file.getOriginalFilename());
        String url = wrkImgService.uploadPicture(file, imgType, wrkId, locationAddress);
        return AjaxResult.success(url);
    }

    /**
     * 删除工单图片
     */
    @DeleteMapping("/delete")
    @SysLog(value = "删除图片", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody @Validated ImageDTO image) {
        wrkImgService.removeImg(image);
        return AjaxResult.success();
    }

}
