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 com.hfkj.sysenum.cms.CmsStatusEnum;
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;

/**
 * @className: CmsContentServiceImpl
 * @author: HuRui
 * @date: 2024/9/24
 **/
@Service("cmsContentService")
public class CmsContentServiceImpl implements CmsContentService {
    @Resource
    private CmsContentMapper cmsContentMapper;
    public final static String CACHE_CMS_ONLINE = "CACHE_CMS_ONLINE";
    @Override
    public void editData(CmsContent data) {
        data.setUpdateTime(new Date());
        if (data.getId() == null) {
            data.setCreateTime(new Date());
            cmsContentMapper.insert(data);
        } else {
            cmsContentMapper.updateByPrimaryKey(data);
        }
    }

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

    @Override
    public List<CmsContent> getList(Map<String, Object> param) {
        CmsContentExample example = new CmsContentExample();
        CmsContentExample.Criteria criteria = example.createCriteria() .andStatusNotEqualTo(CmsStatusEnum.status0.getStatus());

        if (MapUtils.getInteger(param, "type") != null) {
            criteria.andTypeEqualTo(MapUtils.getInteger(param, "type"));
        }

        if (MapUtils.getInteger(param, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        if (StringUtils.isNotBlank(MapUtils.getString(param, "name"))) {
            criteria.andNameLike("%"+MapUtils.getString(param, "name")+"%");
        }

        example.setOrderByClause("update_time desc");
        return cmsContentMapper.selectByExample(example);
    }

}
