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

import java.util.*;

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

/**
 * 渠道销售分配，分销商针对某个商品sku的销售分配Controller
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-04-09 18:04:46
 */
@Controller
@RequestMapping("mkdistsellallocation")
public class MkDistSellAllocationController {
    @Autowired
    private MkDistSellAllocationService mkDistSellAllocationService;

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

        List<MkDistSellAllocationEntity> mkDistSellAllocationList = mkDistSellAllocationService.queryList(query);
        int total = mkDistSellAllocationService.queryTotal(query);

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

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

    /**
     * 查看信息
     */
    @RequestMapping("/info/{sellAllocId}")
    @RequiresPermissions("mkdistsellallocation:info")
    @ResponseBody
    public R info(@PathVariable("sellAllocId") Integer sellAllocId) {
        MkDistSellAllocationEntity mkDistSellAllocation = mkDistSellAllocationService.queryObject(sellAllocId);
        String expendDistId = mkDistSellAllocation.getExpendDistIds();
        String[] expendDistIds = expendDistId.split(",");
        List<Integer> expendDistIdList = new ArrayList<>();
        for (int i=0; i< expendDistIds.length;i++){
            expendDistIdList.add(Integer.valueOf(expendDistIds[i]));
        }
        mkDistSellAllocation.setExpendDistIdList(expendDistIdList);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("mkdistsellallocation:save")
    @ResponseBody
    public R save(@RequestBody MkDistSellAllocationEntity mkDistSellAllocation) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDistSellAllocation);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
//        builder.put("distChnlId", "分销渠道id");
        builder.put("thirdMerchSn", "所属三方商户");
        builder.put("supplierId", "所属供货商");
        builder.put("storeId", "所属门店");
        builder.put("goodsId", "商品id");
        builder.put("distId", "商品所属分销商id");
        builder.put("allocType", "分配方式");
//        builder.put("expendDistLines", "支出提成额度");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkDistSellAllocationService.save(mkDistSellAllocation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("mkdistsellallocation:update")
    @ResponseBody
    public R update(@RequestBody MkDistSellAllocationEntity mkDistSellAllocation) {
        Map<String, Object> valideDate = MapBeanUtil.fromObject(mkDistSellAllocation);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
//        builder.put("distChnlId", "分销渠道id");
        builder.put("thirdMerchSn", "所属三方商户");
        builder.put("supplierId", "所属供货商");
        builder.put("storeId", "所属门店");
        builder.put("goodsId", "商品id");
        builder.put("distId", "商品所属分销商id");
        builder.put("allocType", "分配方式");
//        builder.put("expendDistLines", "支出提成额度");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        mkDistSellAllocationService.update(mkDistSellAllocation);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("mkdistsellallocation:delete")
    @ResponseBody
    public R delete(@RequestBody Integer[]sellAllocIds) {
        mkDistSellAllocationService.deleteBatch(sellAllocIds);

        return R.ok();
    }

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