package com.kmall.admin.controller;

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

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dto.CopyFreightDto;
import com.kmall.admin.entity.FreightEntity;
import com.kmall.admin.entity.FreightItemEntity;
import com.kmall.admin.service.FreightService;
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.*;

/**
 * Controller
 *
 * @author Scott
 * @email
 * @date 2017-08-21 21:19:49
 */
@RestController
@RequestMapping("freight")
public class FreightController {
    @Autowired
    private FreightService freightService;

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

        List<FreightEntity> freightList = freightService.queryList(query);
        int total = freightService.queryTotal(query);

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

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

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

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("freight:save")
    public R save(@RequestBody FreightEntity freight) {

        if (freight.getFreightItemEntityList() == null || freight.getFreightItemEntityList().size() == 0) {
            throw new RRException("配送区域及运费不能为空");
        }

        for (FreightItemEntity freightItemEntity : freight.getFreightItemEntityList()) {
            if (freightItemEntity != null) {
                Map<String, Object> valideDate = MapBeanUtil.fromObject(freightItemEntity);
                ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
                builder.put("firstPiece", "首件");
                builder.put("freight", "运费");
                builder.put("continuePiece", "续件");
                builder.put("renew", "续费");
                R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
                if (Integer.valueOf(r.get("code").toString()) != 0) {
                    throw new RRException(r.get("msg").toString());
                }
            }
        }


        freightService.save(freight);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("freight:update")
    public R update(@RequestBody FreightEntity freight) {

        if (freight.getFreightItemEntityList() == null || freight.getFreightItemEntityList().size() == 0) {
            throw new RRException("配送区域及运费不能为空");
        }

        for (FreightItemEntity freightItemEntity : freight.getFreightItemEntityList()) {
            if (freightItemEntity != null) {
                Map<String, Object> valideDate = MapBeanUtil.fromObject(freightItemEntity);
                ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
                builder.put("firstPiece", "首件");
                builder.put("freight", "运费");
                builder.put("continuePiece", "续件");
                builder.put("renew", "续费");
                R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
                if (Integer.valueOf(r.get("code").toString()) != 0) {
                    throw new RRException(r.get("msg").toString());
                }
            }
        }

        freightService.update(freight);

        return R.ok();
    }

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

        return R.ok();
    }

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

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

        List<FreightEntity> list = freightService.queryList(params);

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

    /**
     * 保存
     */
    @RequestMapping("/saveCopyFreight")
//    @RequiresPermissions("freight:saveCopyFreight")
    public R saveCopyFreight(@RequestBody CopyFreightDto copyFreightDto) {
        freightService.saveCopyFreight(copyFreightDto);
        return R.ok();
    }
}
