package com.chinaitop.depot.intelligent.basicdata.service.impl;

import com.chinaitop.depot.intelligent.basicdata.mapper.BasicStorehouseMapper;
import com.chinaitop.depot.intelligent.basicdata.model.BasicStorehouse;
import com.chinaitop.depot.intelligent.basicdata.model.BasicStorehouseExample;
import com.chinaitop.depot.intelligent.basicdata.service.BasicStorehouseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BasicStorehouseServiceImpl implements BasicStorehouseService {
    @Resource
    BasicStorehouseMapper basicStorehouseMapper;

    @Override
    public boolean getBycfName(String cfName) {
        boolean temp = false;
        List<BasicStorehouse> basicStorehouses = null;
        BasicStorehouseExample example = new BasicStorehouseExample();
        BasicStorehouseExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cfName)) {
            criteria.andStorehouseNameEqualTo(cfName);
            basicStorehouses = basicStorehouseMapper.selectByExample(example);
        }
        if (basicStorehouses != null && basicStorehouses.size() > 0) {
            return temp = true;
        }
        return temp;
    }

    @Override
    public boolean getBycfCode(String cfCode) {
        boolean temp = false;
        List<BasicStorehouse> basicStorehouses = null;
        BasicStorehouseExample example = new BasicStorehouseExample();
        BasicStorehouseExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(cfCode)) {
            criteria.andStorehouseCodeEqualTo(cfCode);
            basicStorehouses = basicStorehouseMapper.selectByExample(example);
        }
        if (basicStorehouses != null && basicStorehouses.size() > 0) {
            return temp = true;
        }
        return temp;
    }

    @Override
    public BasicStorehouse queryOrgNameOrCode(String rogId, String code) {
        BasicStorehouseExample example = new BasicStorehouseExample();
        BasicStorehouseExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(rogId)) {
            criteria.andOrgIdEqualTo(Integer.valueOf(rogId));
        }
        if (StringUtils.isNotBlank(code)) {
            criteria.andStorehouseCodeEqualTo(code);
        }
        List<BasicStorehouse> basicStorehouses = basicStorehouseMapper.selectByExample(example);
        if (basicStorehouses.size() > 0) {
            return basicStorehouses.get(0);
        }
        return null;
    }
}
