package com.mvc.controller.upgrade;

import com.mvc.model.ResPageModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.model.upgrade.TUpgradePackageModel;
import com.mvc.service.upgrade.TUpgradePackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "升级包接口")
@RequestMapping("/TUpgradePackageAction")
@Controller
public class TUpgradePackageAction {

    @Resource
    private TUpgradePackageService tUpgradePackageService;

    @ApiOperation("获取原始文件包列表(未经过差分的)")
    @RequestMapping(value = "/findOriginalPackageList", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findOriginalPackageList(@RequestBody TUpgradePackageModel model) {
        ResultGeekerModel resultModel;
        try {
            List<TUpgradePackageModel> resList = tUpgradePackageService.findOriginalPackageList(model);
            resultModel = new ResultGeekerModel(resList);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("查询所有升级包")
    @RequestMapping(value = "/findAllTUpgradePackage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel findAllTUpgradePackage(@RequestBody TUpgradePackageModel model) {
        ResultGeekerModel resultModel;
        try {
            ResPageModel res = tUpgradePackageService.findAllTUpgradePackage(model);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("新增升级包")
    @RequestMapping(value = "/saveTUpgradePackage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel saveTUpgradePackage(@RequestBody TUpgradePackageModel model) {
        ResultGeekerModel resultModel;
        try {
            TUpgradePackageModel resModel = tUpgradePackageService.saveTUpgradePackage(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("更新升级包")
    @RequestMapping(value = "/updateTUpgradePackage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel updateTUpgradePackage(@RequestBody TUpgradePackageModel model) {
        ResultGeekerModel resultModel;
        try {
            TUpgradePackageModel resModel = tUpgradePackageService.updateTUpgradePackage(model);
            resultModel = new ResultGeekerModel(resModel);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("删除升级包")
    @RequestMapping(value = "/deleteTUpgradePackage", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteTUpgradePackage(int id) {
        ResultGeekerModel resultModel;
        try {
            String info = tUpgradePackageService.verificationRules(id);
            if (StringUtils.isNotEmpty(info)) {
                resultModel = new ResultGeekerModel(info);
            } else {
                int res = tUpgradePackageService.deleteTUpgradePackage(id);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("上传升级包")
    @RequestMapping(value = "/uploadPackage", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel uploadPackage(@RequestParam("file") MultipartFile file) {
        ResultGeekerModel resultModel;
        try {
            TUpgradePackageModel res = tUpgradePackageService.uploadPackage(file);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation("上传升级包(原始文件生成差分包)")
    @RequestMapping(value = "/uploadPackageOriginalFile", method = RequestMethod.POST)
    @ResponseBody
    public ResultGeekerModel uploadPackageOriginalFile(@RequestParam("file") MultipartFile file, @RequestParam("param") String param) {
        ResultGeekerModel resultModel;
        try {
            TUpgradePackageModel res = tUpgradePackageService.uploadPackageOriginalFile(file, param);
            resultModel = new ResultGeekerModel(res);
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
