package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.CmsContent;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.cms.CmsContentReadService;
import com.hfkj.service.cms.CmsContentService;
import com.hfkj.service.cms.impl.CmsContentServiceImpl;
import com.hfkj.sysenum.cms.CmsStatusEnum;
import com.hfkj.sysenum.cms.CmsTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * @className: CmsController
 * @author: HuRui
 * @date: 2024/9/24
 **/
@Controller
@RequestMapping(value = "/cms")
@Api(value = "CMS")
public class CmsController {
    private static Logger log = LoggerFactory.getLogger(CmsController.class);
    @Resource
    private CmsContentService cmsContentService;
    @Resource
    private CmsContentReadService cmsContentReadService;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private UserCenter userCenter;

    @RequestMapping(value="/readAlready",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "已阅读")
    public ResponseData readAlready(@RequestBody JSONObject body) {
        try {
            // 用户信息
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
            if (body == null || body.getLong("contentId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 创建阅读记录
            cmsContentReadService.create(body.getLong("contentId"), userSession.getUser().getId());
            return ResponseMsgUtil.success("操作成功");

        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/query",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData query(@RequestParam(name = "type", required = true) Integer type) {
        try {
            // 用户信息
            UserSessionObject userSession = null;
            try {
                userSession = userCenter.getSessionModel(UserSessionObject.class);
            } catch (Exception e) {
                // 不做任何处理
            }
            List<CmsContent> dataList;

            // 读取缓存
            Object cacheObj = redisUtil.hget(CmsContentServiceImpl.CACHE_CMS_ONLINE, "" + type);
            if (cacheObj != null) {
                dataList = (List<CmsContent>) cacheObj;
            } else {
                Map<String,Object> param = new HashMap<>();
                param.put("type", type);
                param.put("status", CmsStatusEnum.status2.getStatus());
                dataList = cmsContentService.getList(param);
                // 缓存
                redisUtil.hset(CmsContentServiceImpl.CACHE_CMS_ONLINE, "" + type, dataList);
            }
            for (CmsContent data : dataList) {
                if (CmsTypeEnum.type2.getType().equals(data.getType()) && userSession != null) {
                    // 用户是否阅读，阅读过就不展示
                    data.setExt1(""+(!cmsContentReadService.isRead(data.getId(), userSession.getUser().getId())));
                } else {
                    // 展示
                    data.setExt1("true");
                }
            }
            return ResponseMsgUtil.success(dataList);
        } catch (Exception e) {
            return ResponseMsgUtil.exception(e);
        }
    }

}
