package com.kmall.admin.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dao.SysCusNationCodeDao;
import com.kmall.admin.entity.SysCusNationCodeEntity;
import com.kmall.admin.service.SysCusNationCodeService;
import com.kmall.common.constant.TablePrimaryKeyPrefix;
import com.kmall.common.utils.*;
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;
import java.util.Random;


/**
 * 国别地区代码Service实现类
 *
 * @author emato
 * @email admin@qhdswl.com
 * @date 2018-11-10 15:26:44
 */
@Service("sysCusNationCodeService")
public class SysCusNationCodeServiceImpl
        implements SysCusNationCodeService {
    @Autowired
    private SysCusNationCodeDao sysCusNationCodeDao;

    @Override
    public SysCusNationCodeEntity queryObject(String sn) {
        return sysCusNationCodeDao.queryObject(sn);
    }

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

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

    @Override
    public int save(SysCusNationCodeEntity sysCusNationCode) {
        SysCusNationCodeEntity SysCusNationCodeTemp = sysCusNationCodeDao.queryObjectByName(sysCusNationCode.getName());
        if (SysCusNationCodeTemp != null) {
            throw new RRException("名称已经存在");
        }

        if (sysCusNationCode.getCode().length() > 3) {
            throw new RRException("代码长度有误");
        }

        SysCusNationCodeTemp = sysCusNationCodeDao.queryObjectByCode(sysCusNationCode.getCode());
        if (SysCusNationCodeTemp != null) {
            throw new RRException("代码已经存在");
        }

        if (StringUtils.isNotEmpty(sysCusNationCode.getDiscountsTaxType()) &&
            sysCusNationCode.getDiscountsTaxType().length() > 1) {
            throw new RRException("优普税率类型有误");
        }

        sysCusNationCode.setSn(TablePrimaryKeyPrefix.sys_cus_nation_code + RandomUtils.getNum(18));
        return sysCusNationCodeDao.save(sysCusNationCode);
    }

    @Override
    public int update(SysCusNationCodeEntity sysCusNationCode) {
        SysCusNationCodeEntity SysCusNationCodeTemp = sysCusNationCodeDao.queryObjectByName(sysCusNationCode.getName());
        if (SysCusNationCodeTemp != null && !SysCusNationCodeTemp.getSn().equals(sysCusNationCode.getSn())) {
            throw new RRException("名称已经存在");
        }

        if (sysCusNationCode.getCode().length() > 3) {
            throw new RRException("代码长度有误");
        }

        SysCusNationCodeTemp = sysCusNationCodeDao.queryObjectByCode(sysCusNationCode.getCode());
        if (SysCusNationCodeTemp != null && !SysCusNationCodeTemp.getSn().equals(sysCusNationCode.getSn())) {
            throw new RRException("代码已经存在");
        }

        if (StringUtils.isNotEmpty(sysCusNationCode.getDiscountsTaxType()) &&
            sysCusNationCode.getDiscountsTaxType().length() > 1) {
            throw new RRException("优普税率类型有误");
        }
        return sysCusNationCodeDao.update(sysCusNationCode);
    }

    @Override
    public int delete(String sn) {
        return sysCusNationCodeDao.delete(sn);
    }

    @Override
    public int deleteBatch(String[] sns) {
        return sysCusNationCodeDao.deleteBatch(sns);
    }
}
