package com.kmall.admin.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dao.*;
import com.kmall.admin.entity.*;
import com.kmall.admin.service.ProductStoreRelaService;
import com.kmall.common.constant.Dict;
import com.kmall.common.entity.SysUserEntity;
import com.kmall.common.utils.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Service实现类
 *
 * @author Scott
 * @email
 * @date 2017-12-27 19:12:46
 */
@Service("productStoreRelaService")
public class ProductStoreRelaServiceImpl implements ProductStoreRelaService {
    @Autowired
    private ProductStoreRelaDao productStoreRelaDao;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsSpecificationDao goodsSpecificationDao;
    @Autowired
    private ProductDao productDao;
    @Autowired
    private GoodsGalleryDao goodsGalleryDao;

    @Override
    public ProductStoreRelaEntity queryObject(Integer id) {
        return productStoreRelaDao.queryObject(id);
    }

    @Override
    public ProductStoreRelaEntity queryByStoreIdProductId(Long storeId, Long productId) {
        return productStoreRelaDao.queryByStoreIdProductId(storeId, productId);
    }

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

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

    @Override
    public int save(ProductStoreRelaEntity productStoreRela) {
        GoodsEntity goodsEntity = null;
        Map<String, Object> valideDate = MapBeanUtil.fromObject(productStoreRela);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("storeId", "门店");
        builder.put("goodsId", "商品");
        builder.put("stockNum", "库存");

        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        } else {
            goodsEntity = goodsDao.queryObject(productStoreRela.getGoodsId());
            if (Dict.orderBizType.item_11.getItem().equals(goodsEntity.getGoodsBizType())) {
                // 普通商品校验商品规格
                builder.put("specification", "规格");
                builder.put("retailPrice", "零售价");
                builder.put("marketPrice", "市场价");
            }
            r = ValidatorUtil.isEmpty(builder.build(), valideDate);
            if (Integer.valueOf(r.get("code").toString()) != 0) {
                throw new RRException(r.get("msg").toString());
            }
            if(!StringUtils.isNotEmpty(goodsEntity.getListPicUrl())){
                throw new RRException("该商品列表图不能为空！请先在商品管理》所有商品中维护商品编号为【"+goodsEntity.getGoodsSn()+"】的商品列表图信息，再来操作门店商品数据");
            }
            if(!StringUtils.isNotEmpty(goodsEntity.getPrimaryPicUrl())){
                throw new RRException("该商品主图不能为空！请先在商品管理》所有商品中维护商品编号为【"+goodsEntity.getGoodsSn()+"】的商品主图信息，再来操作门店商品数据");
            }
            List<GoodsGalleryEntity> galleryEntityList = goodsGalleryDao.queryObjectByGoodId(goodsEntity.getId());
            if (galleryEntityList == null || galleryEntityList.size() <= 0) {
                throw new RRException("该商品轮播图不能为空！请先在商品管理》所有商品中维护商品编号为【"+goodsEntity.getGoodsSn()+"】的商品详情轮播图信息，再来操作门店商品数据");
            }
        }

        Map<String, Object> map = new HashMap();
        map.put("goodsId", goodsEntity.getId());
        map.put("storeId", productStoreRela.getStoreId());
        List<ProductStoreRelaEntity> storeRelaList = querySameList(map);
        if (storeRelaList != null && storeRelaList.size() > 0) {
            throw new RRException("该门店已存在此商品！");
        }

        SysUserEntity user = ShiroUtils.getUserEntity();
        ProductEntity product = productDao.queryObjectBySn(goodsEntity.getGoodsSn());
        if (Dict.orderBizType.item_11.getItem().equals(goodsEntity.getGoodsBizType())) {
            GoodsEntity goods = new GoodsEntity();
            goods.setRetailPrice(productStoreRela.getRetailPrice());
            goods.setMarketPrice(productStoreRela.getMarketPrice());
            goods.setId(goodsEntity.getId());
            goodsDao.update(goods);

            GoodsSpecificationEntity goodsSpecificationEntity = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
            if(goodsSpecificationEntity != null){
                goodsSpecificationDao.delete(goodsSpecificationEntity.getId());
            }
            // 添加商品规格
            GoodsSpecificationEntity goodsSpecification = new GoodsSpecificationEntity();
            goodsSpecification.setGoodsId(goodsEntity.getId());
            goodsSpecification.setValue(productStoreRela.getSpecification());
            goodsSpecification.setSpecificationId(1);
            goodsSpecificationDao.save(goodsSpecification);

            // 修改产品
            product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
            product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
            productDao.update(product);

        } else {
            productStoreRela.setRetailPrice(goodsEntity.getRetailPrice());
            productStoreRela.setMarketPrice(goodsEntity.getMarketPrice());
        }

        productStoreRela.setProductId(product.getId());
        productStoreRela.setGoodsBizType(goodsEntity.getGoodsBizType());
        productStoreRela.setSku(goodsEntity.getSku());
        productStoreRela.setCreaterSn(user.getUserId().toString());
        productStoreRela.setCreateTime(new Date());
        productStoreRela.setModerSn(user.getUserId().toString());
        productStoreRela.setModTime(new Date());
        return productStoreRelaDao.save(productStoreRela);
    }

    public List<ProductStoreRelaEntity> querySameList(Map<String, Object> map) {
        return productStoreRelaDao.querySameList(map);
    }

    @Override
    public int update(ProductStoreRelaEntity productStoreRela) {
        GoodsEntity goodsEntity = null;
        Map<String, Object> valideDate = MapBeanUtil.fromObject(productStoreRela);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("storeId", "门店");
        builder.put("goodsId", "商品");
        builder.put("stockNum", "库存");

        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        } else {
            goodsEntity = goodsDao.queryObject(productStoreRela.getGoodsId());
            if (Dict.orderBizType.item_11.getItem().equals(goodsEntity.getGoodsBizType())) {
                // 普通商品校验商品规格
                builder.put("specification", "规格");
                builder.put("retailPrice", "零售价");
                builder.put("marketPrice", "市场价");
            }
            r = ValidatorUtil.isEmpty(builder.build(), valideDate);
            if (Integer.valueOf(r.get("code").toString()) != 0) {
                throw new RRException(r.get("msg").toString());
            }
        }

        Map<String, Object> map = new HashMap();
        map.put("goodsId", goodsEntity.getId());
        map.put("storeId", productStoreRela.getStoreId());
        map.put("id", productStoreRela.getId());
        List<ProductStoreRelaEntity> storeRelaList = querySameList(map);
        if (storeRelaList != null && storeRelaList.size() > 0) {
            throw new RRException("该门店已存在此商品！");
        }

        SysUserEntity user = ShiroUtils.getUserEntity();
        if (Dict.orderBizType.item_11.getItem().equals(goodsEntity.getGoodsBizType())) {
            GoodsEntity goods = new GoodsEntity();
            goods.setRetailPrice(productStoreRela.getRetailPrice());
            goods.setMarketPrice(productStoreRela.getMarketPrice());
            goods.setId(goodsEntity.getId());
            goodsDao.update(goods);
            // 添加商品规格
            GoodsSpecificationEntity goodsSpecification = goodsSpecificationDao.queryByGoodsId(goodsEntity.getId());
            goodsSpecification.setValue(productStoreRela.getSpecification());
            goodsSpecificationDao.update(goodsSpecification);

            // 修改产品
            ProductEntity product = productDao.queryObjectBySn(goodsEntity.getGoodsSn());
            product.setGoodsSpecificationIds(goodsSpecification.getId().toString());
            product.setGoodsSpecificationNameValue(goodsSpecification.getValue());
            productDao.update(product);
        } else {
            productStoreRela.setRetailPrice(goodsEntity.getRetailPrice());
        }

        productStoreRela.setModerSn(user.getUserId().toString());
        productStoreRela.setModTime(new Date());
        return productStoreRelaDao.update(productStoreRela);
    }

    @Override
    public int delete(Integer id) {
        return productStoreRelaDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return productStoreRelaDao.deleteBatch(ids);
    }
}
