package com.kmall.admin.controller.mk.dist;

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

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.mk.dist.MkDistEntity;
import com.kmall.admin.service.mk.dist.MkDistService;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 分销商，distributorController
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-04-09 18:04:47
 */
@Controller
@RequestMapping("mkdist")
public class MkDistController {
    @Autowired
    private MkDistService mkDistService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mkdist:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        ParamUtils.setName(params, "distName");
        //查询列表数据
        Query query = new Query(params);
        List<MkDistEntity> mkDistList = mkDistService.queryList(query);
        int total = mkDistService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(mkDistList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{distId}")
    @RequiresPermissions("mkdist:info")
    @ResponseBody
    public R info(@PathVariable("distId") Integer distId) {
        MkDistEntity mkDist = mkDistService.queryObject(distId);
        return R.ok().put("mkDist", mkDist);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkdist:save")
    @ResponseBody
    public R save(@RequestBody MkDistEntity mkDist) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDist);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("distName", "分销商名称");
        builder.put("distEname", "分销商英文名");
        builder.put("distFlag", "分销商标识");
        builder.put("thirdMerchSn", "所属第三方商户");
        builder.put("storeId", "所属门店");
        builder.put("supplierId", "所属供货商");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkDistService.save(mkDist);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkdist:update")
    @ResponseBody
    public R update(@RequestBody MkDistEntity mkDist) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDist);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("distName", "分销商名称");
        builder.put("distEname", "分销商英文名");
        builder.put("distFlag", "分销商标识");
        builder.put("thirdMerchSn", "所属第三方商户");
        builder.put("storeId", "所属门店");
        builder.put("supplierId", "所属供货商");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkDistService.update(mkDist);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mkdist:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]distIds) {
        mkDistService.deleteBatch(distIds);
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        List<MkDistEntity> list = mkDistService.queryList(params);
        return R.ok().put("list", list);
    }

    /**
     * 修改层级时查看上级分销商
     * @param params
     * @return
     */
    @RequestMapping("/queryAllById")
    @ResponseBody
    public R queryAllById(@RequestParam Map<String, Object> params) {
        List<MkDistEntity> list = new ArrayList<>();
        String distId = (String)params.get("distId");
        if(StringUtils.isNotEmpty(distId)){
            MkDistEntity mkDistEntity = mkDistService.queryObject(Integer.parseInt(distId));
            list.add(mkDistEntity);
        }
        return R.ok().put("list", list);
    }

    /**
     * 根据商品id查询分销商
     * @param params
     * @return
     */
    @RequestMapping("/queryDistByGoodsId")
    @ResponseBody
    public R queryDistByGoodsId(@RequestParam Map<String, Object> params) {
        if(StringUtils.isNotEmpty((String)params.get("storeId"))) {
            MkDistEntity mkDistEntity = mkDistService.queryDistByStoreId(params);
            return R.ok().put("mkDistEntity", mkDistEntity);
        }
        return R.ok().put("mkDistEntity", null);

    }

    @RequestMapping("/queryAllByHier")
    @ResponseBody
    public R queryAllByHier(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        params.put("hierLvl", "1");
        List<MkDistEntity> list = mkDistService.queryAllByHier(params);
        return R.ok().put("list", list);
    }

}
