package com.kmall.admin.service.impl;

import com.google.common.collect.Maps;
import com.kmall.admin.dao.OfflineCartDao;
import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.entity.OfflineCartEntity;
import com.kmall.admin.service.OfflineCartService;
import com.kmall.admin.fromcomm.entity.SysUserEntity;
import com.kmall.admin.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2018-11-29 15:14:59
 */
@Service("offlineCartService")
public class OfflineCartServiceImpl implements OfflineCartService {
    @Autowired
    private OfflineCartDao offlineCartDao;

    @Override
    public OfflineCartEntity queryObject(Integer id) {
        return offlineCartDao.queryObject(id);
    }

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

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

    @Override
    public int save(OfflineCartEntity offlineCart) {
        return offlineCartDao.save(offlineCart);
    }

    @Override
    public int update(OfflineCartEntity offlineCart) {
        return offlineCartDao.update(offlineCart);
    }

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

    @Override
    public int deleteByUserId(long userId){
        return offlineCartDao.deleteByUserId(userId);
    }
    @Override
    public int deleteBatch(Integer[]ids) {
        return offlineCartDao.deleteBatch(ids);
    }


    @Override
    public List<OfflineCartEntity> offlineGoodsCart(GoodsEntity goods){
        SysUserEntity user = ShiroUtils.getUserEntity();
        OfflineCartEntity cart = offlineCartDao.queryObjectByGoodsIdAndType(Integer.parseInt(goods.getId().toString()),user.getUserId());
        OfflineCartEntity cartEntity = new OfflineCartEntity();
        cartEntity.setGoodsId(Integer.parseInt(goods.getId().toString()));
        cartEntity.setModTime(new Date());
        if(cart != null){
            cartEntity.setId(cart.getId());
            cartEntity.setNumber(cart.getNumber()+1);
            update(cartEntity);
        }else{
            cartEntity.setUserId(Integer.parseInt(user.getUserId().toString()));
            cartEntity.setGoodsName(goods.getName());
            cartEntity.setGoodsSn(goods.getGoodsSn());
            cartEntity.setProdBarcode(goods.getProdBarcode());
            cartEntity.setSku(goods.getSku());
            cartEntity.setProductId(Integer.parseInt(goods.getProductId()));
            cartEntity.setMarketPrice(goods.getStoreMarketPrice());
            cartEntity.setRetailPrice(goods.getStoreRetailPrice());
            cartEntity.setNumber(1);
            cartEntity.setStockNum(Integer.parseInt(goods.getStockNum()));
            cartEntity.setListPicUrl(goods.getListPicUrl());
            cartEntity.setStoreId(goods.getStoreId());
            cartEntity.setCreateTime(new Date());
            save(cartEntity);
        }
        Map map= Maps.newHashMap();
        map.put("userId",user.getUserId());
        map.put("storeId",goods.getStoreId());
        return offlineCartDao.queryOfflineCartByBizType(map);
    }
}
