package com.hfkj.service.cms.impl;

import com.hfkj.dao.CmsContentMapper;
import com.hfkj.entity.CmsContent;
import com.hfkj.entity.CmsContentExample;
import com.hfkj.service.cms.CmsContentService;
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("cmsContentService")
public class CmsContentServiceImpl implements CmsContentService {

    @Resource
    private CmsContentMapper cmsContentMapper;

    @Override
    public void create(CmsContent cmsContent) {
        cmsContentMapper.insert(cmsContent);
    }

    @Override
    public void update(CmsContent cmsContent) {
        cmsContentMapper.updateByPrimaryKeySelective(cmsContent);
    }

    @Override
    public void delete(Long id, Boolean fullDelete) {
        cmsContentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CmsContent queryDetail(Long id) {
        return cmsContentMapper.selectByPrimaryKey(id);
    }

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

    @Override
    public List<CmsContent> getList(Map<String, Object> map) {

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

        if (StringUtils.isNotBlank(MapUtils.getString(map, "code"))) {
            criteria.andCodeEqualTo(MapUtils.getString(map, "code"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "name"))) {
            criteria.andNameEqualTo(MapUtils.getString(map, "name"));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "platformCode"))) {
            criteria.andPlatformCodeEqualTo(MapUtils.getString(map, "platformCode"));
        }
        example.setOrderByClause("create_time desc");
        return cmsContentMapper.selectByExample(example);
    }
}
