package com.jsh.erp.controller.apkVersion;


import com.jsh.erp.base.AjaxResult;
import com.jsh.erp.base.BaseController;
import com.jsh.erp.base.TableDataInfo;
import com.jsh.erp.datasource.entities.ApkVersion;
import com.jsh.erp.service.ApkVersionService;
import com.jsh.erp.service.SystemConfigService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/apkVersion")
@Api(tags = {"apk版本管理接口"})
public class apkVersionController extends BaseController {

    @Resource
    private ApkVersionService apkVersionService;

    @ApiOperation("apk版本列表")
    @PostMapping("/list")
    public TableDataInfo list(){
        startPage();
        List<ApkVersion> list = apkVersionService.list();
        return getDataTable(list);
    }

    @ApiOperation("新增apk版本")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ApkVersion apkVersion) {
        apkVersion.setCreateTime(new Date());
        boolean b = apkVersionService.save(apkVersion);
        if (!b){
            return AjaxResult.error("创建失败，请联系系统管理员");
        }
        return AjaxResult.success();
    }

    /**
     * apk文件上传方法
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/upload")
    @ApiOperation(value = "apk文件上传方法")
    public BaseResponseInfo upload(HttpServletRequest request, HttpServletResponse response) {
        BaseResponseInfo res = new BaseResponseInfo();
        try {
            String savePath = "";
            String bizPath = request.getParameter("biz");
            if ("bill".equals(bizPath) || "financial".equals(bizPath) || "material".equals(bizPath)) {
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultipartFile file = multipartRequest.getFile("file");// 获取上传文件对象
                savePath = apkVersionService.uploadLocal(file, request);
                if(StringUtil.isNotEmpty(savePath)){
                    res.code = 200;
                    res.data = savePath;
                }else {
                    res.code = 500;
                    res.data = "上传失败！";
                }
            } else {
                res.code = 505;
                res.data = "文件分类错误！";
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            res.code = 500;
            res.data = "上传失败！";
        }
        return res;
    }

}
