package com.hai.service.impl;

import com.hai.dao.HighBrandMapper;
import com.hai.entity.*;
import com.hai.service.HighBrandService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * @Auther: 袁野
 * @Description:
 * @Date: 2021/3/26 23:06
 */
@Service("highBrandService")
public class HighBrandServiceImpl implements HighBrandService {

    @Resource
    private HighBrandMapper highBrandMapper;

    @Override
    public List<HighBrand> getListBand(Map<String, String> map) {

        HighBrandExample example = new HighBrandExample();
        HighBrandExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getString(map, "title") != null) {
            criteria.andTitleLike("%" + MapUtils.getString(map, "title") + "%");
        }

        if (MapUtils.getString(map, "goodTypeId") != null) {
            criteria.andGoodTypeIdEqualTo(MapUtils.getIntValue(map, "goodTypeId"));
        }

        criteria.andStatusEqualTo(1);

        example.setOrderByClause("created_time desc");

        return highBrandMapper.selectByExample(example);
    }

    @Override
    public HighBrand findById(Integer id) {
        return highBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean findByTitle(String title , Integer goodsTypeId) {
        HighBrandExample example = new HighBrandExample();
        HighBrandExample.Criteria criteria = example.createCriteria();

        criteria.andTitleEqualTo(title).andStatusEqualTo(1).andGoodTypeIdEqualTo(goodsTypeId);

        List<HighBrand> highBrands = highBrandMapper.selectByExample(example);

        return highBrands.size() > 0;
    }

    @Override
    public void insertBrand(HighBrand highBrand) {
        highBrandMapper.insert(highBrand);
    }

    @Override
    public void updateBrand(HighBrand highBrand) {
        highBrandMapper.updateByPrimaryKey(highBrand);
    }
}
