package cn.pluss.platform.controller.officialGoods;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.entity.OfficialGoods;
import cn.pluss.platform.officialGoods.OfficialGoodsService;
import cn.pluss.platform.util.OssOperatUtil;
import cn.pluss.platform.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.List;
import java.util.Map;

/**
 * <p>
 * 商品活动表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-09-29
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/officialGoods")
public class OfficialGoodsController extends BaseModelController<OfficialGoodsService, OfficialGoods> {

    private final OfficialGoodsService OfficialGoodsService;

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

    @RequestMapping("add")
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("officialGoods/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("officialGoods/detail");
        OfficialGoods v = OfficialGoodsService.getById(id);
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        view.addObject("uploadParam",uploadParam);
        view.addObject("officialGoods",v);
        return view;
    }

    @RequestMapping("page")
    @ResponseBody
    public Result<Page<OfficialGoods>> page(Page<OfficialGoods> page, OfficialGoods officialGoods) {
        QueryWrapper<OfficialGoods> queryWrapper = new QueryWrapper<OfficialGoods>().orderByDesc("createTime");
        if(StringUtil.isNotEmpty(officialGoods.getGoodName())){
            queryWrapper.like("goodName",officialGoods.getGoodName());
        }
        if(StringUtil.isNotEmpty(officialGoods.getStatus())){
            queryWrapper.eq("status",officialGoods.getStatus());
        }
        page = OfficialGoodsService.page(page, queryWrapper);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody OfficialGoods officialGoods) {

        if (officialGoods.getId() != null){
            OfficialGoodsService.updateById(officialGoods);
        }else {
            officialGoods.setCreateTime(new Date());
            OfficialGoodsService.save(officialGoods);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }

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

    @PostMapping("batchDelete")
    @ResponseBody
    public Result<String> batchDelete(@RequestBody List<Integer> ids) {
        OfficialGoodsService.removeByIds(ids);
        return ResultGenerator.genSuccessResult("操作成功!");
    }
}
