package cn.pluss.platform.controller.appVersionInfo;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.appVersionInfo.AppVersionInfoService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.AppVersionInfo;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.PageUtils;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.Map;

@Controller
@RequiredArgsConstructor
@RequestMapping("appVersionInfo")
public class AppVersionInfoController extends BaseNoModelController {

    private final AppVersionInfoService appVersionInfoService;

    @GetMapping("index")
    public String index() {
        return "appVersionInfo/list";
    }

    @RequestMapping("add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("appVersionInfo/detail");
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        return view;
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id){
        ModelAndView view = new ModelAndView("appVersionInfo/detail");
        AppVersionInfo v = appVersionInfoService.getById(id);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        view.addObject("appVerInfo",v);
        return view;
    }

    @GetMapping("page")
    @ResponseBody
    public Result<PageInfo<AppVersionInfo>> page(Page<AppVersionInfo> page,AppVersionInfo appVersionInfo) {
        Page<AppVersionInfo> pageData= appVersionInfoService.pageData(page,appVersionInfo);
        return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody AppVersionInfo appVersionInfo) {
        if (appVersionInfo.getId()!= 0){
            appVersionInfo.setUpdateTime(new Date());
            appVersionInfoService.updateById(appVersionInfo);
        }else {
            appVersionInfo.setCreateTime(new Date());
            appVersionInfo.setUpdateTime(new Date());
            appVersionInfoService.save(appVersionInfo);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @PostMapping("delete")
    @ResponseBody
    public Result<String> delete(@RequestBody AppVersionInfo appVersionInfo) {
        appVersionInfoService.removeById(appVersionInfo.getId());
        if(StringUtil.isNotEmpty(appVersionInfo.getUrl())){
            OssOperatUtil.deleteFile(appVersionInfo.getUrl());
        }
        return ResultGenerator.genSuccessResult("操作成功!");
    }
}