package com.kmall.admin.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dao.FreightDao;
import com.kmall.admin.dao.FreightItemDao;
import com.kmall.admin.dao.GoodsDao;
import com.kmall.admin.entity.FreightEntity;
import com.kmall.admin.entity.FreightItemEntity;
import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.service.FreightService;
import com.kmall.common.utils.MapBeanUtil;
import com.kmall.common.utils.R;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2018-10-22 15:18:16
 */
@Service("freightService")
public class FreightServiceImpl
        implements FreightService {
    @Autowired
    private FreightDao freightDao;
    @Autowired
    private FreightItemDao freightItemDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public FreightEntity queryObject(Integer id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("freId", id);
        List<FreightItemEntity> freightItemEntityList = freightItemDao.queryList(map);
        FreightEntity freightEntity = freightDao.queryObject(id);
        freightEntity.setFreightItemEntityList(freightItemEntityList);
        return freightEntity;
    }

    @Override
    public List<FreightEntity> queryList(Map<String, Object> map) {
        return freightDao.queryList(map);
    }

    @Override
    public List<FreightEntity> queryEntity(Map<String, Object> map) {
        return freightDao.queryEntity(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return freightDao.queryTotal(map);
    }

    @Override
    @Transactional
    public int save(FreightEntity freight) {
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", freight.getStoreId());
        map.put("merchSn", freight.getMerchSn());
        map.put("name", freight.getName());
        List<FreightEntity> list = queryList(map);
        if (list != null && list.size() != 0) {
            throw new RRException("运费模版名称已存在！");
        }

        Map<String, Object> valideDate = MapBeanUtil.fromObject(freight);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        if (Boolean.valueOf(freight.getIsDefault())) {
            freight.setIsDefault("1");
        } else {
            freight.setIsDefault("0");
        }

        // 运费详情
        List<FreightItemEntity> freightItemEntityList = freight.getFreightItemEntityList();
        if (freight.getFreightItemEntityList() == null || freight.getFreightItemEntityList().size() == 0) {
            throw new RRException("配送区域及运费不能为空");
        }

        freight.setDefaultFreight(freightItemEntityList.get(0).getFreight());
        int res = freightDao.save(freight);

        for (FreightItemEntity item : freightItemEntityList) {
            if (Boolean.valueOf(item.getDeliveryArea())) {
                item.setDeliveryArea("ALL");
            }
            if (item.getFreId() == null) {
                item.setFreId(freight.getId());
                freightItemDao.save(item);
            }
        }
        return res;
    }

    @Override
    @Transactional
    public int update(FreightEntity freight) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", freight.getId());
        map.put("storeId", freight.getStoreId());
        map.put("merchSn", freight.getMerchSn());
        map.put("name", freight.getName());
        List<FreightEntity> list = queryEntity(map);
        if (list != null && list.size() != 0) {
            throw new RRException("运费模版名称已存在！");
        }

        Map<String, Object> valideDate = MapBeanUtil.fromObject(freight);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }

        if (Boolean.valueOf(freight.getIsDefault())) {
            freight.setIsDefault("1");
        } else {
            freight.setIsDefault("0");
        }

        // 运费详情
        if (freightItemDao.deleteByFreId(freight.getId()) <= 0) {
            throw new RRException("删除原运费模版详情失败！");
        }
        List<FreightItemEntity> freightItemEntityList = freight.getFreightItemEntityList();
        if (null != freightItemEntityList && freightItemEntityList.size() > 0) {
            freight.setDefaultFreight(freightItemEntityList.get(0).getFreight());
            for (FreightItemEntity item : freightItemEntityList) {
                if (Boolean.valueOf(item.getDeliveryArea())) {
                    item.setDeliveryArea("ALL");
                }
                if (item.getFreId() != null) {
                    freightItemDao.save(item);
                }
            }
        }
        return freightDao.update(freight);
    }

    @Override
    public int delete(Integer id) {
        Map<String, Object> map = new HashMap<>();
        map.put("freightId", id);
        List<GoodsEntity> list = goodsDao.queryList(map);

        if (list != null && list.size() > 0) {
            throw new RRException("此运费模版还有【" + list.size() + "】商品正在使用，不能删除！");
        }
        if (freightItemDao.deleteByFreId(id) <= 0) {
            throw new RRException("删除此运费模版详情失败！");
        }

        return freightDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        for (int id : ids) {
            Map<String, Object> map = new HashMap<>();
            map.put("freightId", id);
            map.put("isDelete", 0);
            List<GoodsEntity> list = goodsDao.queryList(map);

            if (list != null && list.size() > 0) {
                throw new RRException("【" + id + "】运费模版还有【" + list.size() + "】个商品正在使用，不能删除！");
            }
            if (freightItemDao.deleteByFreId(id) <= 0) {
                throw new RRException("删除【" + id + "】运费模版详情失败！");
            }
        }
        return freightDao.deleteBatch(ids);
    }
}
