package com.hai.service.impl;

import com.hai.dao.HighGasDiscountOilPriceMapper;
import com.hai.entity.HighGasDiscountOilPrice;
import com.hai.entity.HighGasDiscountOilPriceExample;
import com.hai.service.HighGasDiscountOilPriceService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("highGasDiscountOilPriceService")
public class HighGasDiscountOilPriceServiceImpl implements HighGasDiscountOilPriceService {

    @Resource
    private HighGasDiscountOilPriceMapper highGasDiscountOilPriceMapper;

    @Override
    public void editGasDiscountOilPrice(HighGasDiscountOilPrice highGasDiscountOilPrice) {
        if (highGasDiscountOilPrice.getId() != null) {
            highGasDiscountOilPriceMapper.updateByPrimaryKey(highGasDiscountOilPrice);
        } else {
            highGasDiscountOilPriceMapper.insert(highGasDiscountOilPrice);
        }
    }

    @Override
    public void delete(Long id) {
        highGasDiscountOilPriceMapper.deleteByPrimaryKey(id);
    }

    @Override
    public HighGasDiscountOilPrice getDetail(Long id) {
        return highGasDiscountOilPriceMapper.selectByPrimaryKey(id);
    }

    @Override
    public HighGasDiscountOilPrice getDetailByOilNo(String oilNo) {
        HighGasDiscountOilPriceExample example = new HighGasDiscountOilPriceExample();
        example.createCriteria().andOilNoEqualTo(oilNo);
        List<HighGasDiscountOilPrice> list = highGasDiscountOilPriceMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public HighGasDiscountOilPrice getDetailByOilNoName(String oilNoName) {
        HighGasDiscountOilPriceExample example = new HighGasDiscountOilPriceExample();
        example.createCriteria().andOilNoNameEqualTo(oilNoName);
        List<HighGasDiscountOilPrice> list = highGasDiscountOilPriceMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<HighGasDiscountOilPrice> getList(Map<String,Object> map) {
        HighGasDiscountOilPriceExample example = new HighGasDiscountOilPriceExample();
        HighGasDiscountOilPriceExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(MapUtils.getString(map, "oilNo"))) {
            criteria.andOilNoEqualTo(MapUtils.getString(map, "oilNo"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "oilNoName"))) {
            criteria.andOilNoNameEqualTo(MapUtils.getString(map, "oilNoName"));
        }
        example.setOrderByClause("create_time desc");
        return highGasDiscountOilPriceMapper.selectByExample(example);
    }
}
