package com.kmall.admin.controller.mk;

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

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.mk.MkCenterMenuStoreRealEntity;
import com.kmall.admin.service.mk.MkCenterMenuStoreRealService;
import com.kmall.common.utils.*;
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.*;


/**
 * 营销中心菜单门店关联表Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-05-28 10:14:06
 */
@Controller
@RequestMapping("mkcentermenustorereal")
public class MkCenterMenuStoreRealController {
    @Autowired
    private MkCenterMenuStoreRealService mkCenterMenuStoreRealService;

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

        List<MkCenterMenuStoreRealEntity> mkCenterMenuStoreRealList = mkCenterMenuStoreRealService.queryList(query);
        int total = mkCenterMenuStoreRealService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("mkcentermenustorereal:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        MkCenterMenuStoreRealEntity mkCenterMenuStoreReal = mkCenterMenuStoreRealService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkcentermenustorereal:save")
    @ResponseBody
    public R save(@RequestBody MkCenterMenuStoreRealEntity mkCenterMenuStoreReal) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkCenterMenuStoreReal);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("thirdMerchSn", "所属三方商户");
//        builder.put("storeId", "所属门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkCenterMenuStoreRealService.saveOrUpdate(mkCenterMenuStoreReal);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkcentermenustorereal:update")
    @ResponseBody
    public R update(@RequestBody MkCenterMenuStoreRealEntity mkCenterMenuStoreReal) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkCenterMenuStoreReal);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("thirdMerchSn", "所属三方商户");
//        builder.put("storeId", "所属门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkCenterMenuStoreRealService.update(mkCenterMenuStoreReal);

        return R.ok();
    }

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

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<MkCenterMenuStoreRealEntity> list = mkCenterMenuStoreRealService.queryList(params);

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