package com.pjilisense.flxai.controller;

import com.pjilisense.flxai.base.constant.Constant;
import com.pjilisense.flxai.page.PageData;
import com.pjilisense.flxai.utils.FileUtil;
import com.pjilisense.flxai.utils.Result;
import com.pjilisense.flxai.dto.DigitalImgDTO;
import com.pjilisense.flxai.service.DigitalImgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import springfox.documentation.annotations.ApiIgnore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;


/**
 * ${comments}
 *
 * @author liushujing liushujing@philisense.com
 * @since 1.0.0 2025-02-26
 */
@RestController
@RequestMapping(Constant.BASE_PATH+"digitalimg")
@Api(tags="${comments}")
public class DigitalImgController {

    private static Logger logger = LoggerFactory.getLogger(DigitalImgController.class);

    @Autowired
    private DigitalImgService digitalImgService;

    @Autowired
    FileUtil fileUtil;
    @PostMapping("/uploadPicture")
    public Result<String> uploadPicture(@RequestParam(value="file") MultipartFile file) {
        if (file.isEmpty()) {
            return new Result<String>().error("文件为空");
        }
        try {
            String relfilename =fileUtil.uploadFile(file);
            return new Result<String>().ok(relfilename);
        } catch (RuntimeException e) {
            return new Result<String>().error("上传失败: " + e.getMessage());
        }
    }
    @GetMapping("/getPicture")
    public void getPicture(HttpServletResponse response,@RequestParam(name = "id", required = true) String id) {
        try {
            DigitalImgDTO data = digitalImgService.get(id);
            if(StringUtils.hasText(data.getImgPath())) {
                fileUtil.downLoadFile(data.getImgPath(), null, response);
            }
        } catch (RuntimeException e) {
        }
    }

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="Integer", dataTypeClass =Integer.class) ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="Integer", dataTypeClass =Integer.class) ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String", dataTypeClass =String.class) ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String", dataTypeClass =String.class)
    })
    public Result<PageData<DigitalImgDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<DigitalImgDTO> page = digitalImgService.page(params);

        return new Result<PageData<DigitalImgDTO>>().ok(page);
    }

    @GetMapping("getAllByType")
    @ApiOperation("根据类型获取全部数据")
    public Result<List<DigitalImgDTO>> getAllByType(@ApiIgnore @RequestParam String imgType){
        List<DigitalImgDTO> page = digitalImgService.getAllByType(imgType);

        return new Result<List<DigitalImgDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    public Result<DigitalImgDTO> get(@PathVariable("id") String id){
        DigitalImgDTO data = digitalImgService.get(id);

        return new Result<DigitalImgDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    public Result save(@RequestBody DigitalImgDTO dto){
        digitalImgService.save(dto);

        return new Result();
        //param ={"imgPath":"uploads/202503/5c799cffa5864619b817063f77fe734d..png","name":"开源项目","imgType":"1"}
        //{"code":0,"msg":"success","data":null}
    }

    @PutMapping
    @ApiOperation("修改")
    public Result update(@RequestBody DigitalImgDTO dto){
        digitalImgService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    public Result delete(@RequestBody String[] ids){

        digitalImgService.delete(ids);

        return new Result();
    }

}