package com.hfkj.service.cms.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.RedisUtil;
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.apache.rocketmq.spring.core.RocketMQTemplate;
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;
    @Resource
    private RedisUtil redisUtil;
    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 void online(Long id) {
        // 查询内容
        CmsContent cmsContent = getDetail(id);
        if (cmsContent == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        if (CmsStatusEnum.status2.getStatus().equals(cmsContent.getStatus())) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法上线，状态错误");
        }
        cmsContent.setStatus(CmsStatusEnum.status2.getStatus());
        editData(cmsContent);
        // 删除缓存
        redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType());
    }

    @Override
    public void offline(Long id) {
        // 查询内容
        CmsContent cmsContent = getDetail(id);
        if (cmsContent == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        if (!CmsStatusEnum.status2.getStatus().equals(cmsContent.getStatus())) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法下线，状态错误");
        }
        cmsContent.setStatus(CmsStatusEnum.status3.getStatus());
        editData(cmsContent);

        // 删除缓存
        redisUtil.hdel(CmsContentServiceImpl.CACHE_CMS_ONLINE, ""+cmsContent.getType());
    }

    @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")+"%");
        }

        if (MapUtils.getLong(param, "createTimeS") != null) {
            criteria.andCreateTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeS")));
        }

        if (MapUtils.getLong(param, "createTimeE") != null) {
            criteria.andCreateTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "createTimeE")));
        }

        if (MapUtils.getLong(param, "startTimeS") != null) {
            criteria.andStartTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "startTimeS")));
        }

        if (MapUtils.getLong(param, "startTimeE") != null) {
            criteria.andStartTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "startTimeE")));
        }

        if (MapUtils.getLong(param, "endTimeS") != null) {
            criteria.andEndTimeGreaterThanOrEqualTo(new Date(MapUtils.getLong(param, "endTimeS")));
        }

        if (MapUtils.getLong(param, "endTimeE") != null) {
            criteria.andEndTimeLessThanOrEqualTo(new Date(MapUtils.getLong(param, "endTimeE")));
        }

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

}
