package com.kmall.admin.controller.mk;

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

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.entity.mk.MkDistChnlEntity;
import com.kmall.admin.service.mk.MkDistChnlService;
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.*;

/**
 * 分销渠道Controller
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2019-04-09 18:04:47
 */
@Controller
@RequestMapping("mkdistchnl")
public class MkDistChnlController {
    @Autowired
    private MkDistChnlService mkDistChnlService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("mkdistchnl:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");
        //查询列表数据
        Query query = new Query(params);

        List<MkDistChnlEntity> mkDistChnlList = mkDistChnlService.queryList(query);
        int total = mkDistChnlService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{distChnlId}")
    @RequiresPermissions("mkdistchnl:info")
    @ResponseBody
    public R info(@PathVariable("distChnlId") Integer distChnlId) {
        MkDistChnlEntity mkDistChnl = mkDistChnlService.queryObject(distChnlId);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkdistchnl:save")
    @ResponseBody
    public R save(@RequestBody MkDistChnlEntity mkDistChnl) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDistChnl);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("distFlag", "分销渠道名称");
//        builder.put("partType", "所属分类");
        builder.put("thirdMerchSn", "所属第三方商户");
        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());
        }
        mkDistChnlService.save(mkDistChnl);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkdistchnl:update")
    @ResponseBody
    public R update(@RequestBody MkDistChnlEntity mkDistChnl) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDistChnl);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("distFlag", "分销渠道名称");
//        builder.put("partType", "所属分类");
        builder.put("thirdMerchSn", "所属第三方商户");
        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());
        }
        mkDistChnlService.update(mkDistChnl);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mkdistchnl:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]distChnlIds) {
        mkDistChnlService.deleteBatch(distChnlIds);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {
        ParamUtils.setQueryPowerByRoleType(params, "storeId", "merchSn", "thirdPartyMerchCode");

        List<MkDistChnlEntity> list = mkDistChnlService.queryList(params);

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