package com.kmall.admin.service.impl;

import com.kmall.admin.dao.MerchDao;
import com.kmall.admin.dao.StoreDao;
import com.kmall.admin.dao.ThirdMerchantBizDao;
import com.kmall.admin.entity.MerchEntity;
import com.kmall.admin.entity.StoreEntity;
import com.kmall.admin.entity.ThirdMerchantBizEntity;
import com.kmall.admin.service.StoreService;
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;
    @Autowired
    private ThirdMerchantBizDao thirdMerchantBizDao;

    @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) {
        if(StringUtils.isNotEmpty(store.getMerchSn())){
            MerchEntity merchEntity = merchDao.findByMerchSn(store.getMerchSn());
            if(merchEntity != null){
                store.setMerchName(merchEntity.getMerchName());
            }else{
                throw new RRException("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }else{
            throw new RRException("商户编号不能为空");
        }
        if(StringUtils.isNotEmpty(store.getThirdPartyMerchCode())){
            ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(store.getThirdPartyMerchCode());
            if(thirdMerchantBizEntity != null){
                store.setThirdPartyMerchName(thirdMerchantBizEntity.getThirdPartyMerchName());
            }else{
                throw new RRException("第三方商户编号【"+store.getThirdPartyMerchCode()+"】不存在，请先维护第三方商户信息再来操作");
            }
        }else{
            throw new RRException("第三方商户编号不能为空");
        }

        StoreEntity storeEntity = storeDao.queryObjectByName(store.getStoreName());
        if(storeEntity != null){
            throw new RRException("门店名称已存在");
        }
//        if(StringUtils.isNotEmpty(String.valueOf(store.getDistId()))) {
//            StoreEntity storeEntityByDistId = storeDao.queryObjectByDistId(store.getDistId());
//            if (storeEntityByDistId != null) {
//                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("商户编号不存在，请先维护用户商户编号信息再来操作");
            }
        }
        if(StringUtils.isEmpty(store.getThirdPartyMerchCode())){
            throw new RRException("第三方商户编号不能为空");
        }else{
            ThirdMerchantBizEntity thirdMerchantBizEntity = thirdMerchantBizDao.getThirdMerchangByCode(store.getThirdPartyMerchCode());
            if(thirdMerchantBizEntity != null){
                store.setThirdPartyMerchName(thirdMerchantBizEntity.getThirdPartyMerchName());
            }else{
                throw new RRException("第三方商户编号【"+store.getThirdPartyMerchCode()+"】不存在，请先维护第三方商户信息再来操作");
            }
        }

        StoreEntity storeEntity = storeDao.queryObjectByName(store.getStoreName());
        if(storeEntity != null && !storeEntity.getId().equals(store.getId())){
            throw new RRException("门店名称已存在");
        }
//        if(StringUtils.isNotEmpty(String.valueOf(store.getDistId()))) {
//            StoreEntity storeEntityByDistId = storeDao.queryObjectByDistId(store.getDistId());
//            if (storeEntityByDistId != null && !storeEntityByDistId.getId().equals(store.getId())) {
//                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);
    }
}
