package cn.pluss.platform.controller.appGuide;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.appGuide.AppGuideService;
import cn.pluss.platform.entity.AppGuide;
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.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
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;

/**
 * @author djh
 */
@Controller
@RequestMapping("/appGuide")
public class AppGuideController {

    @Autowired
    private AppGuideService appGuideService;

    @GetMapping("/index")
    public String index() {
        return "appGuide/appGuideList";
    }

    @RequestMapping("add")
    public String add() {
        return "appGuide/appGuideDetail";
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id){
        ModelAndView view = new ModelAndView("appGuide/appGuideDetail");
        AppGuide v = appGuideService.getById(id);
        view.addObject("entity",v);
        return view;
    }

    @GetMapping("page")
    @ResponseBody
    public Result<PageInfo<AppGuide>> page(Page<AppGuide> page, AppGuide condition) {
        Page<AppGuide> pageData= appGuideService.page(page, new QueryWrapper<>(condition));
        return ResultGenerator.genSuccessResult(PageUtils.page(pageData));
    }

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

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