package com.hai.service.impl;

import com.hai.dao.ApiMemberProductMapper;
import com.hai.entity.ApiMemberProduct;
import com.hai.entity.ApiMemberProductExample;
import com.hai.service.ApiMemberProductService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

@Service("apiMemberProductService")
public class ApiMemberProductServiceImpl implements ApiMemberProductService {

    @Resource
    private ApiMemberProductMapper apiMemberProductMapper;

    @Override
    public void insertApiMemberProduct(ApiMemberProduct apiMemberProduct) {
        apiMemberProductMapper.insert(apiMemberProduct);
    }

    @Override
    public void updateApiMemberProduct(ApiMemberProduct apiMemberProduct) {
        apiMemberProductMapper.updateByPrimaryKeySelective(apiMemberProduct);
    }

    @Override
    public List<ApiMemberProduct> getListApiMemberProduct(Map<String, Object> map) {
        ApiMemberProductExample example = new ApiMemberProductExample();
        ApiMemberProductExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getInteger(map , "typeId") != null) {
            criteria.andTypeIdEqualTo(MapUtils.getInteger(map , "typeId"));
        }
        if (MapUtils.getInteger(map , "brandId") != null) {
            criteria.andBrandIdEqualTo(MapUtils.getInteger(map , "brandId"));
        }
        if (MapUtils.getString(map , "name") != null) {
            criteria.andNameLike("%" + MapUtils.getString(map , "name") + "%");
        }
        example.setOrderByClause("brand_id ASC , sort ASC");
        return apiMemberProductMapper.selectByExample(example);
    }

    @Override
    public ApiMemberProduct findById(Long id) {
        return apiMemberProductMapper.selectByPrimaryKey(id);
    }
}
