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

import com.kmall.admin.dao.mk.dist.MkDistDao;
import com.kmall.admin.dao.mk.dist.MkDistHierDao;
import com.kmall.admin.entity.mk.dist.MkDistEntity;
import com.kmall.admin.entity.mk.dist.MkDistHierEntity;
import com.kmall.admin.service.mk.dist.MkDistService;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分销商，distributorService实现类
 *
 * @author huangyq
 * @email admin@qhdswl.com
 * @date 2019-04-09 18:04:47
 */
@Service("mkDistService")
public class MkDistServiceImpl implements MkDistService {
    @Autowired
    private MkDistDao mkDistDao;
    @Autowired
    private MkDistHierDao mkDistHierDao;

    @Override
    public MkDistEntity queryObject(Integer distId) {
        return mkDistDao.queryObject(distId);
    }

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

    @Override
    public List<MkDistEntity> queryAllByHier(Map<String, Object> map) {
        return mkDistDao.queryAllByHier(map);
    }


    @Override
    public MkDistEntity queryDistByGoodsId(Integer goodsId,Integer storeId){
        return mkDistDao.queryDistByGoodsId(goodsId,storeId);
    }

    @Override
    public MkDistEntity queryDistByStoreId(Map<String, Object> map){
        return mkDistDao.queryDistByStoreId(Integer.parseInt((String)map.get("storeId")));
    }
    @Override
    public int queryTotal(Map<String, Object> map) {
        return mkDistDao.queryTotal(map);
    }

    @Override
    public int save(MkDistEntity mkDist) {
        MkDistEntity mkDistEntity = mkDistDao.queryObjectByDistName(mkDist.getDistName());
        if (mkDistEntity != null) {
            throw new RRException("分销商名称已经存在");
        }
        MkDistEntity distEntity = mkDistDao.queryDistByStoreId(mkDist.getStoreId());
        if (distEntity != null) {
            throw new RRException("该门店已已创建分销商关系，请重新选择门店");
        }

        mkDist.setCreaterSn(ShiroUtils.getUserEntity().getUsername());
        mkDist.setCreateTime(new Date());
        return mkDistDao.save(mkDist);
    }

    @Override
    public int update(MkDistEntity mkDist) {
        MkDistEntity mkDistEntity = mkDistDao.queryObjectByDistName(mkDist.getDistName());
        if (mkDistEntity != null && !mkDistEntity.getDistId().equals(mkDist.getDistId())) {
            throw new RRException("分销商名称已经存在");
        }
        MkDistEntity distEntity = mkDistDao.queryDistByStoreId(mkDist.getStoreId());
        if (distEntity != null && !distEntity.getDistId().equals(mkDist.getDistId())) {
            throw new RRException("该门店已已创建分销商关系，请重新选择门店");
        }
        mkDist.setModerSn(ShiroUtils.getUserEntity().getUsername());
        mkDist.setModTime(new Date());
        return mkDistDao.update(mkDist);
    }

    @Override
    public int delete(Integer distId) {
        return mkDistDao.delete(distId);
    }

    @Override
    public int deleteBatch(Integer[] distIds) {
        List<String> distNameList = new ArrayList<>();
        for (int i=0; i<distIds.length;i++){
            MkDistHierEntity mkDistHierEntity = mkDistHierDao.selectDistHierByDistId(distIds[i]);
            if (mkDistHierEntity != null) {
                MkDistEntity mkDistEntity = mkDistDao.queryObject(distIds[i]);
                distNameList.add(mkDistEntity.getDistName());
            }
        }
        if(distNameList.size() > 0 ){
            throw new RRException("当前删除的分销商"+distNameList+"已创建层级关系，不能删除");
        }
        return mkDistDao.deleteBatch(distIds);
    }
}
