package com.kmall.admin.service.impl;

import com.kmall.admin.dao.MerchDao;
import com.kmall.admin.dao.SupplierDao;
import com.kmall.admin.entity.MerchEntity;
import com.kmall.admin.entity.SupplierEntity;
import com.kmall.admin.service.SupplierService;
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 emato
 * @email admin@qhdswl.com
 * @date 2018-11-26 15:14:24
 */
@Service("supplierService")
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private MerchDao merchDao;

    @Override
    public SupplierEntity queryObject(Integer id) {
        return supplierDao.queryObject(id);
    }

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

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

    @Override
    public int save(SupplierEntity supplier) {
        if(StringUtils.isEmpty(supplier.getLevelMerchSn())){
            throw new RRException("一级商户编号不能为空");
        }else{
            MerchEntity merchEntity = merchDao.findByMerchSn(supplier.getLevelMerchSn());
            if(merchEntity != null){
                supplier.setLevelMerchFlag(merchEntity.getMerchShortName());
            }else{
                throw new RRException("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }
        return supplierDao.save(supplier);
    }

    @Override
    public int update(SupplierEntity supplier) {
        if(StringUtils.isEmpty(supplier.getLevelMerchSn())){
            throw new RRException("一级商户编号不能为空");
        }else{
            MerchEntity merchEntity = merchDao.findByMerchSn(supplier.getLevelMerchSn());
            if(merchEntity != null){
                supplier.setLevelMerchFlag(merchEntity.getMerchShortName());
            }else{
                throw new RRException("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }
        return supplierDao.update(supplier);
    }

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

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