package cn.pluss.platform.controller.banner;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.banner.BannerService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.Banner;
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.core.conditions.update.UpdateWrapper;
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.Map;

@Controller
@RequestMapping("banner")
public class BannerController extends BaseNoModelController {

    @Autowired
    private BannerService bannerService;

    /**
     * 引导页
     *
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "banner/bannerList";
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @ResponseBody
    public Result<PageInfo<Banner>> queryHelpVideoByPage(Page<Banner> page) {
        QueryWrapper<Banner> bannerQueryWrapper = new QueryWrapper<Banner>().orderByDesc("seq");
        page = bannerService.page(page, bannerQueryWrapper);
        return ResultGenerator.genSuccessResult(PageUtils.page(page));
    }

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

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody Banner banner) {
        if (banner.getId() == null) {
            bannerService.save(banner);
        } else {
            bannerService.updateById(banner);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

    @RequestMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable Integer id) {
        ModelAndView view = new ModelAndView("banner/bannerBaseInfo");
        Banner banner = bannerService.getById(id);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("banner", banner);
        view.addObject("uploadParam", uploadParam);
        return view;
    }

    @PostMapping("/statusUpdate")
    @ResponseBody
    public Result<String> statusUpdate(@RequestBody Banner banner) {
        UpdateWrapper<Banner> bannerUpdateWrapper = new UpdateWrapper<Banner>().eq("id", banner.getId());
        if (StringUtil.isEmpty(banner.getStatus()) || banner.getStatus().equals("1")) {
            banner.setStatus("0");
            bannerService.update(banner, bannerUpdateWrapper);
        } else {
            banner.setStatus("1");
            bannerService.update(banner, bannerUpdateWrapper);
        }
        return ResultGenerator.genSuccessResult("操作成功!");
    }

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

}
