package cn.pluss.platform.controller.bestNewActivity;

import cn.pluss.platform.PushService;
import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.bestNewActivity.BestNewActivityService;
import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.BestNewActivity;
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 lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

@Controller
@RequestMapping("bestNewActivity")
public class BestNewActivityController extends BaseNoModelController {

    @Autowired
    private BestNewActivityService bestNewActivityService;

    @Setter(onMethod_ = {@Autowired})
    private PushService pushService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping("/index")
    public String index() {
        return "bestNewActivity/bestNewActivityList";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<PageInfo<BestNewActivity>> page(Page<BestNewActivity> page, BestNewActivity bestNewActivity) {
        QueryWrapper<BestNewActivity> bestNewActivityQueryWrapper = new QueryWrapper<BestNewActivity>().orderByAsc("seq");
        if (StringUtil.isNotEmpty(bestNewActivity.getTitle())) {
            bestNewActivityQueryWrapper.eq("title", bestNewActivity.getTitle());
        }
        page = bestNewActivityService.page(page, bestNewActivityQueryWrapper);
        return ResultGenerator.genSuccessResult(PageUtils.page(page));
    }

    //数据回显
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (StringUtil.isNotEmpty(id)) {
            BestNewActivity bestNewActivity = bestNewActivityService.getById(id);
            request.setAttribute("entity", bestNewActivity);
        }
        return "bestNewActivity/bestNewActivityBaseInfo";
    }

    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public Result<BestNewActivity> saveOrUpdate(@RequestBody BestNewActivity bestNewActivity) {

        if (StringUtil.isEmpty(bestNewActivity.getId())) {
            bestNewActivity.setUpdateTime(new Date());
            ShiroUser shiroUser = queryCurrentShiroUser();
            bestNewActivity.setUpdateor(shiroUser.getLoginName());
            bestNewActivityService.save(bestNewActivity);
            String pushType = bestNewActivity.getPushType();
            if(Objects.equals(pushType, "1")){
                pushService.pushAll(bestNewActivity.getTitle(), bestNewActivity.getDesc());
            }
        } else {
            UpdateWrapper<BestNewActivity> updateWrapper = new UpdateWrapper<BestNewActivity>().eq("id", bestNewActivity.getId());
            bestNewActivityService.update(bestNewActivity, updateWrapper);
        }
        return ResultGenerator.genSuccessResult("保存成功", bestNewActivity);
    }

    @PostMapping("/delete")
    public Result<BestNewActivity> delete(@RequestBody BestNewActivity bestNewActivity) {
        bestNewActivityService.removeById(bestNewActivity.getId());
        return ResultGenerator.genSuccessResult("删除成功！", bestNewActivity);
    }
}