package com.bweb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
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.mqtopic.TaskTopic;
import com.hfkj.service.cms.CmsContentService;
import com.hfkj.service.cms.impl.CmsContentServiceImpl;
import com.hfkj.sysenum.cms.CmsStatusEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
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.Date;
import java.util.HashMap;
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;

    public static void main(String[] args) {
        System.out.println(new Date().compareTo(new Date()));
    }

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "编辑")
    public ResponseData edit(@RequestBody CmsContent body) {
        try {
            if (body == null
                || body.getType() == null
                || StringUtils.isBlank(body.getImgUrl())
            ) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }
            if (body.getStartTime() != null && body.getStartTime().compareTo(new Date()) < 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "开始时间，必须大于当前时间");
            }
            if (body.getEndTime() != null && body.getEndTime().compareTo(body.getStartTime()) < 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "结束时间，必须大于开始时间");
            }

            CmsContent cmsContent = null;
            if (body.getId() != null) {
                // 查询内容
                cmsContent = cmsContentService.getDetail(body.getId());
                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.status1.getStatus());
            } else {
                cmsContent = new CmsContent();
                cmsContent.setStatus(CmsStatusEnum.status1.getStatus());
            }
            cmsContent.setType(body.getType());
            cmsContent.setImgUrl(body.getImgUrl());
            cmsContent.setJumpType(body.getJumpType());
            cmsContent.setJumpUrl(body.getJumpUrl());
            cmsContent.setStartTime(body.getStartTime());
            cmsContent.setEndTime(body.getEndTime());
            cmsContentService.editData(cmsContent);

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/online",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上线")
    public ResponseData online(@RequestBody CmsContent body) {
        try {
            if (body == null || body.getId() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }

            cmsContentService.online(body.getId());

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/offline",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "下线")
    public ResponseData offline(@RequestBody CmsContent body) {
        try {
            if (body == null || body.getId() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }

            cmsContentService.offline(body.getId());

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/delete",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除")
    public ResponseData delete(@RequestBody CmsContent body) {
        try {
            if (body == null || body.getId() == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR);
            }
            // 查询内容
            CmsContent cmsContent = cmsContentService.getDetail(body.getId());
            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.status0.getStatus());
            cmsContentService.editData(cmsContent);

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value="/getDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询详情")
    public ResponseData getDetail(@RequestParam(name = "id", required = true) Long id) {
        try {

            return ResponseMsgUtil.success(cmsContentService.getDetail(id));

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

    @RequestMapping(value="/queryList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询列表")
    public ResponseData queryList(@RequestParam(name = "name", required = false) String name,
                                  @RequestParam(name = "type", required = false) Integer type,
                                  @RequestParam(name = "status", required = false) Integer status,
                                  @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                  @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            Map<String,Object> param = new HashMap<>();
            param.put("name", name);
            param.put("type", type);
            param.put("status", status);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(cmsContentService.getList(param)));

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

}
