package com.kmall.admin.service.impl.mk;

import com.kmall.admin.dao.mk.MkDistChnlDao;
import com.kmall.admin.entity.mk.MkDistChnlEntity;
import com.kmall.admin.service.mk.MkDistChnlService;
import com.kmall.admin.utils.ShiroUtils;
import com.kmall.common.utils.RRException;
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 2019-04-09 18:04:47
 */
@Service("mkDistChnlService")
public class MkDistChnlServiceImpl implements MkDistChnlService {
    @Autowired
    private MkDistChnlDao mkDistChnlDao;

    @Override
    public MkDistChnlEntity queryObject(Integer distChnlId) {
        return mkDistChnlDao.queryObject(distChnlId);
    }

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

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

    @Override
    public int save(MkDistChnlEntity mkDistChnl) {
        MkDistChnlEntity mkDistChnlEntity = mkDistChnlDao.queryObjectByDistFlag(mkDistChnl.getDistFlag());
        if (mkDistChnlEntity != null) {
            throw new RRException("分销渠道名称已经存在");
        }
        mkDistChnl.setCreaterSn(ShiroUtils.getUserEntity().getUsername());
        mkDistChnl.setCreateTime(new Date());
        return mkDistChnlDao.save(mkDistChnl);
    }

    @Override
    public int update(MkDistChnlEntity mkDistChnl) {
        MkDistChnlEntity mkDistChnlEntity = mkDistChnlDao.queryObjectByDistFlag(mkDistChnl.getDistFlag());
        if (mkDistChnlEntity != null && !mkDistChnlEntity.getDistChnlId().equals(mkDistChnl.getDistChnlId())) {
            throw new RRException("分销渠道名称已经存在");
        }
        mkDistChnl.setModerSn(ShiroUtils.getUserEntity().getUsername());
        mkDistChnl.setModTime(new Date());

        return mkDistChnlDao.update(mkDistChnl);
    }

    @Override
    public int delete(Integer distChnlId) {
        return mkDistChnlDao.delete(distChnlId);
    }

    @Override
    public int deleteBatch(Integer[]distChnlIds) {
        return mkDistChnlDao.deleteBatch(distChnlIds);
    }
}
