package com.ruoyi.pda.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.IpUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pda.domain.PdaApkVersion;
import com.ruoyi.pda.service.IPdaApkVersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 手持版本升级Controller
 * 
 * @author ruoyi
 * @date 2023-04-06
 */
@Controller
@RequestMapping("/pda/pda_version")
public class PdaApkVersionController extends BaseController
{
    private String prefix = "pda/pda_version";

    @Autowired
    private IPdaApkVersionService pdaApkVersionService;

    @RequiresPermissions("pda:pda_version:view")
    @GetMapping()
    public String pda_version()
    {
        return prefix + "/pda_version";
    }

    /**
     * 查询手持版本升级列表
     */
    @RequiresPermissions("pda:pda_version:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PdaApkVersion pdaApkVersion)
    {
        startPage();
        List<PdaApkVersion> list = pdaApkVersionService.selectPdaApkVersionList(pdaApkVersion);
        return getDataTable(list);
    }

    /**
     * 导出手持版本升级列表
     */
    @RequiresPermissions("pda:pda_version:export")
    @Log(title = "手持版本升级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PdaApkVersion pdaApkVersion)
    {
        List<PdaApkVersion> list = pdaApkVersionService.selectPdaApkVersionList(pdaApkVersion);
        ExcelUtil<PdaApkVersion> util = new ExcelUtil<PdaApkVersion>(PdaApkVersion.class);
        return util.exportExcel(list, "手持版本升级数据");
    }

    /**
     * 新增手持版本升级
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存手持版本升级
     */
    @RequiresPermissions("pda:pda_version:add")
    @Log(title = "手持版本升级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MultipartFile file, PdaApkVersion pdaApkVersion)
    {

        // 上传文件路径
        String filePath = RuoYiConfig.getUploadPath()+"/apk";
        // 上传并返回新文件名称
        try {
            String fileName = FileUploadUtils.upload(filePath, file);
//            System.out.println("上传路径"+filePath+fileName);
//          D:/ruoyi/uploadPath/upload/profile/upload/2023/04/06/app-debug_20230406170208A001.apk
            String pathname = filePath + fileName;
            System.out.println("地址1"+pathname);
            pathname= pathname.replace("/profile/upload/apk","");
            System.out.println("地址2："+pathname);
            File file1=new File(pathname);
            System.out.println("文件获取："+file1.exists());
            System.out.println(file1.length()/1024);
            pdaApkVersion.setApkSize(file1.length()/1024);
            pdaApkVersion.setApkMd5(Md5Utils.getFileMD5(file1));
            pdaApkVersion.setDownloadUrl("http://10.32.128.150:90"+fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return toAjax(pdaApkVersionService.insertPdaApkVersion(pdaApkVersion));

    }

    /**
     * 修改手持版本升级
     */
    @RequiresPermissions("pda:pda_version:edit")
    @GetMapping("/edit/{objid}")
    public String edit(@PathVariable("objid") Long objid, ModelMap mmap)
    {
        PdaApkVersion pdaApkVersion = pdaApkVersionService.selectPdaApkVersionByObjid(objid);
        mmap.put("pdaApkVersion", pdaApkVersion);
        return prefix + "/edit";
    }

    /**
     * 修改保存手持版本升级
     */
    @RequiresPermissions("pda:pda_version:edit")
    @Log(title = "手持版本升级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PdaApkVersion pdaApkVersion)
    {
        return toAjax(pdaApkVersionService.updatePdaApkVersion(pdaApkVersion));
    }

    /**
     * 删除手持版本升级
     */
    @RequiresPermissions("pda:pda_version:remove")
    @Log(title = "手持版本升级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(pdaApkVersionService.deletePdaApkVersionByObjids(ids));
    }
}
