package com.kmall.admin.controller;

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

import com.kmall.admin.entity.MerchEntity;
import com.kmall.admin.service.MerchService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
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 huangyq
 * @email admin@qhdswl.com
 * @date 2018-12-06 09:41:34
 */
@Controller
@RequestMapping("merch")
public class MerchController {
    @Autowired
    private MerchService merchService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("merch:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeKey", "roleMerchSn", null);

        //查询列表数据
        Query query = new Query(params);

        List<MerchEntity> merchList = merchService.queryList(query);
        int total = merchService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("merch:info")
    @ResponseBody
    public R info(@PathVariable("id") Integer id) {
        MerchEntity merch = merchService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("merch:save")
    @ResponseBody
    public R save(@RequestBody MerchEntity merch) {
        merchService.save(merch);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("merch:update")
    @ResponseBody
    public R update(@RequestBody MerchEntity merch) {
        merchService.update(merch);

        return R.ok();
    }

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

        return R.ok();
    }

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

        ParamUtils.setQueryPowerByRoleType(params, "storeKey", "roleMerchSn", null);

        List<MerchEntity> list = merchService.queryList(params);

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