package com.kmall.admin.controller;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.service.StoreService;
import com.kmall.admin.utils.ParamUtils;
import com.kmall.common.entity.SysUserEntity;
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.*;

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) {

        ParamUtils.setQueryPowerByRoleType(params, "id", "merchSn", false);
        //查询列表数据
        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) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if(user == null) {
            return R.error("用户登录超时，请重新登录");
        }
        if (user.getRoleType().equalsIgnoreCase("2")) {
            return R.error("当前用户角色为【店员】，店员不能新增其他门店信息");
        }
        Map<String, Object> valideDate = MapBeanUtil.fromObject(store);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeName", "门店名称");
        builder.put("storeNumber", "门店编号");
        builder.put("thirdPartyMerchCode", "第三方商户编号");
        builder.put("thirdPartyMerchName", "第三方商户名称");
        builder.put("latitude", "纬度");
        builder.put("longitude", "经度");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        storeService.save(store);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:update")
    @ResponseBody
    public R update(@RequestBody StoreEntity store) {
        SysUserEntity user = ShiroUtils.getUserEntity();
        if(user == null) {
            return R.error("用户登录超时，请重新登录");
        }
        Map<String, Object> valideDate = MapBeanUtil.fromObject(store);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeName", "门店名称");
        builder.put("storeNumber", "门店编号");
        builder.put("thirdPartyMerchCode", "第三方商户编号");
        builder.put("thirdPartyMerchName", "第三方商户名称");
        builder.put("latitude", "纬度");
        builder.put("longitude", "经度");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        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) {

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

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

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


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

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

        List<StoreEntity> list = storeService.queryList(params);
        return R.ok().put("list", list);
    }
}
