package com.kmall.admin.controller.mk;

import java.util.List;
import java.util.Map;

import com.kmall.admin.entity.mk.MkCenterMenuEntity;
import com.kmall.admin.service.mk.MkCenterMenuService;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import com.kmall.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 营销中心菜单表Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-04-15 11:23:38
 */
@Controller
@RequestMapping("mkcentermenu")
public class MkCenterMenuController {
    @Autowired
    private MkCenterMenuService mkCenterMenuService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mkcentermenu:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<MkCenterMenuEntity> mkCenterMenuList = mkCenterMenuService.queryList(query);
        int total = mkCenterMenuService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(mkCenterMenuList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mkcentermenu:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        MkCenterMenuEntity mkCenterMenu = mkCenterMenuService.queryObject(id);

        return R.ok().put("mkCenterMenu", mkCenterMenu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkcentermenu:save")
    @ResponseBody
    public R save(@RequestBody MkCenterMenuEntity mkCenterMenu) {
        mkCenterMenuService.save(mkCenterMenu);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkcentermenu:update")
    @ResponseBody
    public R update(@RequestBody MkCenterMenuEntity mkCenterMenu) {
        mkCenterMenuService.update(mkCenterMenu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mkcentermenu:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[] ids) {
        mkCenterMenuService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params,HttpServletRequest request) {
        String requestPath = request.getRequestURL().toString().replaceAll("mkcentermenu/queryAll","");
        List<MkCenterMenuEntity> list = mkCenterMenuService.queryList(params);
        for (MkCenterMenuEntity mkCenterMenuEntity: list) {
            if(StringUtils.isNotEmpty(mkCenterMenuEntity.getMenuUrl())) {
                mkCenterMenuEntity.setMenuUrl(requestPath + mkCenterMenuEntity.getMenuUrl());
            }
        }

        return R.ok().put("list", list);
    }
}
