package com.kmall.admin.service.impl;

import com.kmall.admin.dao.SysCusUnitCodeDao;
import com.kmall.admin.entity.SysCusNationCodeEntity;
import com.kmall.admin.entity.SysCusUnitCodeEntity;
import com.kmall.admin.service.SysCusUnitCodeService;
import com.kmall.common.constant.TablePrimaryKeyPrefix;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.RandomUtils;
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-10 15:34:36
 */
@Service("sysCusUnitCodeService")
public class SysCusUnitCodeServiceImpl
        implements SysCusUnitCodeService {
    @Autowired
    private SysCusUnitCodeDao sysCusUnitCodeDao;

    @Override
    public SysCusUnitCodeEntity queryObject(String sn) {
        return sysCusUnitCodeDao.queryObject(sn);
    }

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

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

    @Override
    public int save(SysCusUnitCodeEntity sysCusUnitCode) {

        SysCusUnitCodeEntity SysCusUnitCodeTemp = sysCusUnitCodeDao.queryObjectByName(sysCusUnitCode.getName());
        if (SysCusUnitCodeTemp != null) {
            throw new RRException("名称已经存在");
        }

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

        SysCusUnitCodeTemp = sysCusUnitCodeDao.queryObjectByCode(sysCusUnitCode.getCode());
        if (SysCusUnitCodeTemp != null) {
            throw new RRException("代码已经存在");
        }

        sysCusUnitCode.setSn(TablePrimaryKeyPrefix.sys_cus_unit_code + RandomUtils.getNum(18));

        return sysCusUnitCodeDao.save(sysCusUnitCode);
    }

    @Override
    public int update(SysCusUnitCodeEntity sysCusUnitCode) {
        SysCusUnitCodeEntity SysCusUnitCodeTemp = sysCusUnitCodeDao.queryObjectByName(sysCusUnitCode.getName());
        if (SysCusUnitCodeTemp != null && !SysCusUnitCodeTemp.getSn().equals(sysCusUnitCode.getSn())) {
            throw new RRException("名称已经存在");
        }

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

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

        return sysCusUnitCodeDao.update(sysCusUnitCode);
    }

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

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