package com.app.ydl.marketing.controller;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.http.ContentType;
import com.alibaba.fastjson.JSON;
import com.app.framework.core.exception.ValidException;
import com.app.framework.core.support.controller.CrudControllerSupport;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.utils.file.UploadUtil;
import com.app.ydl.marketing.utils.QiniuUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

/**
 * 文件上传下载管理
 */
@Controller
@RequestMapping("/files")
public class FileSiteController extends CrudControllerSupport {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, null, new CustomDateEditor(dateFormat, true));
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

    @Resource
    QiniuUtils qiniuUtils;

    /**
     * 文件上传
     * 单文件
     *
     * @param uploadConfig 上传参数
     */
    @RequestMapping(value = "/upload")
    public void upload(MultipartHttpServletRequest request, HttpServletResponse response, UploadUtil.UploadConfig uploadConfig) {
        AjaxResult ajxResult = AjaxResult.wrap(false);
        try {
            Iterator<String> ite = request.getFileNames();
            while (ite.hasNext()) {
                MultipartFile multipartFile = request.getFile(ite.next());
                if (multipartFile != null) {
                    long size = multipartFile.getSize();
                    if (uploadConfig.getFileSingleSizeLimit() > 0 && size > uploadConfig.getFileSingleSizeLimit()) {//单个文件大小超过限制
                        throw new ValidException("文件大小超过限制");
                    }
                    String url = qiniuUtils.putObject(multipartFile.getInputStream(), multipartFile.getOriginalFilename());
                    //返回上传结果
                    HashMap<String, String> map = new HashMap<>(2);
                    map.put("url", url);
                    ajxResult.setCode(true);
                    ajxResult.setMsg("上传成功");
                    ajxResult.setData(map);
                }
            }
        } catch (Exception e) {
            ajxResult.setMsg(e.getMessage());
        }
        ServletUtil.write(response, JSON.toJSONString(ajxResult), ContentType.JSON.toString(StandardCharsets.UTF_8));
    }

}
