package cn.pluss.platform.controller.merchantQrcode;


import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.MerchantQrcode;
import cn.pluss.platform.merchantQrcode.MerchantQrcodeService;
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 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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-10-23
 */
@Controller
@RequestMapping("/merchantQrcode")
public class MerchantQrcodeController{

    @Autowired
    private MerchantQrcodeService merchantQrcodeService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index() {
        return "merchantQrcode/list";
    }

    @GetMapping("/page")
    @ResponseBody
    public Result<Page<MerchantQrcode>> queryMerchantQrcodePage(Page<MerchantQrcode> page, MerchantQrcode merchantQrcode) {
        QueryWrapper<MerchantQrcode> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(merchantQrcode.getUserId())) {
            wrapper.eq("userId", merchantQrcode.getUserId());
        }
        if (StringUtil.isNotEmpty(merchantQrcode.getMerchantName())) {
            wrapper.eq("merchantName", merchantQrcode.getMerchantName());
        }
        if (StringUtil.isNotEmpty(merchantQrcode.getName())) {
            wrapper.like("userId", merchantQrcode.getName());
        }
        wrapper.orderByDesc( "id");
        Page<MerchantQrcode> result = merchantQrcodeService.page(page, wrapper);
        return ResultGenerator.genSuccessResult(result);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        if (id != null) {
            MerchantQrcode qrcode = new MerchantQrcode().setId(id);
            qrcode = merchantQrcodeService.getOne(new QueryWrapper<>(qrcode));
            request.setAttribute("entity", qrcode);
        }
        Map<String, String> uploadParam = OssOperatUtil.getUploadParam();
        request.setAttribute("uploadParam",uploadParam);
        return "merchantQrcode/detail";
    }
}
