package com.kmall.admin.controller;

import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.service.StoreService;
import com.kmall.common.entity.SysUserEntity;
import com.kmall.common.utils.PageUtils;
import com.kmall.common.utils.Query;
import com.kmall.common.utils.R;
import com.kmall.common.utils.ShiroUtils;
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 java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-12-02 00:53:32
 */
@Controller
@RequestMapping("store")
public class StoreController {
    @Autowired
    private StoreService storeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if(user.getRoleType().equalsIgnoreCase("2")){
            params.put("id", user.getStoreId());
        }
        //查询列表数据
        Query query = new Query(params);

        List<StoreEntity> storeList = storeService.queryList(query);
        int total = storeService.queryTotal(query);

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

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

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

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("store:save")
    @ResponseBody
    public R save(@RequestBody StoreEntity store) {
        storeService.save(store);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:update")
    @ResponseBody
    public R update(@RequestBody StoreEntity store) {
        storeService.update(store);

        return R.ok();
    }

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

        return R.ok();
    }

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

        List<StoreEntity> list = storeService.queryList(params);

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