package cn.pluss.platform.controller.merchantImage;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.MerchantImage;
import cn.pluss.platform.merchantImage.MerchantImageService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("merchantImage")
public class MerchantImageController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private MerchantImageService merchantImageService;

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

        return "merchantImage/merchantImageList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryMerchantImageByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryMerchantImageByPage(Integer currPage, Integer id) {

        if (currPage == null) {
            currPage = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        map.put("id", id);
        List<MerchantImage> merchantImageList = merchantImageService.queryMerchantImagePage(map);
        Integer count = merchantImageService.queryMerchantImagePageCount(map);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("merchantImageList", merchantImageList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantImageDetails", method = RequestMethod.GET)
    public String queryMerchantImageDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MerchantImage merchantImage = new MerchantImage();
            merchantImage.setId(id);
            merchantImage = merchantImageService.queryMerchantImage(merchantImage);
            request.setAttribute("merchantImage", merchantImage);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "merchantImage/merchantImageDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantImageBaseInfo", method = RequestMethod.GET)
    public String queryMerchantImageBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            MerchantImage merchantImage = new MerchantImage();
            merchantImage.setId(id);
            merchantImage = merchantImageService.queryMerchantImage(merchantImage);
            request.setAttribute("merchantImage", merchantImage);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "merchantImage/merchantImageBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantImageExtendsInfo", method = RequestMethod.GET)
    public String queryMerchantImageExtendsInfo() {

        return "merchantImage/merchantImageExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryMerchantImageFunctionsInfo", method = RequestMethod.GET)
    public String queryMerchantImageFunctionsInfo() {

        return "merchantImage/merchantImageFunctionsInfo";
    }

    /**
     * 保存对象
     *
     */
    @RequestMapping(value = "/saveMerchantImage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveMerchantImage(MerchantImage merchantImage) {

        if (merchantImage.getId() != null && merchantImage.getId() != 0) {
            merchantImageService.updateById(merchantImage);
        } else {
            merchantImageService.save(merchantImage);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     */
    @RequestMapping(value = "/deleteMerchantImage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteMerchantImage(Integer id) {
        merchantImageService.removeById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}


