package com.kmall.admin.controller;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.AttributeEntity;
import com.kmall.admin.service.AttributeService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.utils.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-08-17 16:48:17
 */
@RestController
@RequestMapping("attribute")
public class AttributeController {
    @Autowired
    private AttributeService attributeService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("attribute:list")
    public R list(@RequestParam Map<String, Object> params) {

        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);

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

        List<AttributeEntity> attributeList = attributeService.queryList(query);
        int total = attributeService.queryTotal(query);

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

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


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("attribute:info")
    public R info(@PathVariable("id") Integer id) {
        AttributeEntity attribute = attributeService.queryObject(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("attribute:save")
    public R save(@RequestBody AttributeEntity attribute) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(attribute);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("name", "名称");
        builder.put("attributeCategoryId", "所属分类");
        builder.put("merchSn", "商户");
        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());
        }

        attributeService.save(attribute);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("attribute:update")
    public R update(@RequestBody AttributeEntity attribute) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(attribute);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("name", "名称");
        builder.put("attributeCategoryId", "所属分类");
        builder.put("merchSn", "商户");
        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());
        }

        attributeService.update(attribute);
        return R.ok();
    }

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

        return R.ok();
    }

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

        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);

        List<AttributeEntity> list = attributeService.queryList(params);

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

    @RequestMapping("/query")
    public R query(@RequestParam Map<String, Object> params) {

        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", false);

        List<AttributeEntity> list = attributeService.queryList(params);

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