package com.kmall.admin.service.impl;

import com.kmall.admin.dao.MerchDao;
import com.kmall.admin.dao.StoreDao;
import com.kmall.admin.entity.MerchEntity;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.service.StoreService;
import com.kmall.api.service.merch.OmsMerchPropertiesBuilder;
import com.kmall.common.utils.RRException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Service实现类
 *
 * @author Scott
 * @email
 * @date 2017-12-02 00:53:32
 */
@Service("storeService")
public class StoreServiceImpl implements StoreService {
    @Autowired
    private StoreDao storeDao;
    @Autowired
    private MerchDao merchDao;

    @Override
    public StoreEntity queryObject(Integer id) {
        return storeDao.queryObject(id);
    }

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

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

    @Override
    public int save(StoreEntity store) {
//        store.setMerchSn(OmsMerchPropertiesBuilder.instance().getMerchSn());
//        store.setMerchName(OmsMerchPropertiesBuilder.instance().getMerchName());
        if(StringUtils.isEmpty(store.getMerchSn())){
            throw new RRException("商户编号不能为空");
        }else{
            MerchEntity merchEntity = merchDao.findByMerchSn(store.getMerchSn());
            if(merchEntity != null){
                store.setMerchName(merchEntity.getMerchName());
            }else{
                throw new RRException("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }
        return storeDao.save(store);
    }

    @Override
    public int update(StoreEntity store) {
        if(StringUtils.isEmpty(store.getMerchSn())){
            throw new RRException("商户编号不能为空");
        }else{
            MerchEntity merchEntity = merchDao.findByMerchSn(store.getMerchSn());
            if(merchEntity != null){
                store.setMerchName(merchEntity.getMerchName());
            }else{
                throw new RRException("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }
        return storeDao.update(store);
    }

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

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