package com.hfkj.service.goods.impl;

import com.hfkj.dao.GoodsBrandMapper;
import com.hfkj.entity.GoodsBrand;
import com.hfkj.entity.GoodsBrandExample;
import com.hfkj.service.goods.GoodsBrandService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

@Service("goodsBrandService")
public class GoodsBrandServiceImpl implements GoodsBrandService {

    @Resource
    private GoodsBrandMapper goodsBrandMapper;

    @Override
    public void create(GoodsBrand GoodsBrand) {
        goodsBrandMapper.insert(GoodsBrand);
    }

    @Override
    public void update(GoodsBrand GoodsBrand) {
        goodsBrandMapper.updateByPrimaryKey(GoodsBrand);
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        if (fullDelete) {
            goodsBrandMapper.deleteByPrimaryKey(id);
        } else {
            GoodsBrand goodsBrand = queryDetail(id);
            goodsBrand.setStatus(0);
            goodsBrand.setUpdateTime(new Date());
            update(goodsBrand);
        }
    }

    @Override
    public GoodsBrand queryDetail(Long id) {
        return goodsBrandMapper.selectByPrimaryKey(id);
    }

    @Override
    public GoodsBrand queryDetailByMap(Map<String, Object> map) {
        return null;
    }

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

        if (StringUtils.isNotBlank(MapUtils.getString(map, "title"))) {
            criteria.andTitleLike("%"+MapUtils.getString(map, "title")+"%");
        }

        if (StringUtils.isNotBlank(MapUtils.getString(map, "ext1"))) {
            criteria.andExt1EqualTo(MapUtils.getString(map, "ext1"));
        }

        criteria.andStatusNotEqualTo(0);

        return goodsBrandMapper.selectByExample(example);
    }

    @Override
    public String findGoodsBrandName(Long id) {

        GoodsBrand goodsBrand = queryDetail(id);

        return goodsBrand.getTitle();
    }
}
