package com.usky.system.controller.web;


import com.usky.common.core.bean.ApiResult;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.log.annotation.Log;
import com.usky.common.log.enums.BusinessType;
import com.usky.system.domain.MceReceive;
import com.usky.system.service.MceReceiveService;
import com.usky.system.service.vo.MceReceiveResponseVO;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 * 消息接收表 前端控制器
 * </p>
 *
 * @author han
 * @since 2024-04-19
 */
@RestController
@RequestMapping("/mceReceive")
public class MceReceiveController {

    @Autowired
    private MceReceiveService mceReceiveService;

    /**
     * 消息列表分页
     * @param infoTitle  消息标题
     * @param infoType  消息类型
     * @param startTime  开始时间
     * @param endTime  开始时间
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @GetMapping("/mceList")
    public ApiResult<CommonPage<Object>> mceList(@RequestParam(value = "infoTitle", required = false) String infoTitle,
                                                 @RequestParam(value = "infoType", required = false) String infoType,
                                                 @RequestParam(value = "startTime", required = false) String startTime,
                                                 @RequestParam(value = "endTime", required = false) String endTime,
                                                 @RequestParam(value = "id", required = false) Integer id,
                                                 @RequestParam(value = "infoId", required = false) Integer infoId,
                                                 @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                 @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ApiResult.success(mceReceiveService.mceList(infoTitle, infoType, startTime, endTime, id, infoId, current, size));
    }

    /**
     * 消息管理
     * @param infoTitle  消息标题
     * @param infoType  消息标题
     * @param startTime  开始时间
     * @param endTime  开始时间
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @GetMapping("/mceManageList")
    public ApiResult<CommonPage<Object>> mceManageList(@RequestParam(value = "infoTitle", required = false) String infoTitle,
                                                       @RequestParam(value = "infoType", required = false) String infoType,
                                                       @RequestParam(value = "startTime", required = false) String startTime,
                                                       @RequestParam(value = "endTime", required = false) String endTime,
                                                       @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                       @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ApiResult.success(mceReceiveService.mceManageList(infoTitle, infoType, startTime, endTime, current, size));
    }

    /**
     * 消息管理通过ID查询接口
     * @param id  消息内容主键ID
     * @param current      当前页
     * @param size     每页条数
     * @return
     */
    @GetMapping("/mceManageById")
    public ApiResult<CommonPage<MceReceive>> mceManageById(@RequestParam(value = "id") Integer id,
                                                           @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        return ApiResult.success(mceReceiveService.mceManageById(id, current, size));
    }

    /**
     * 消息接收分类统计接口
     * @return
     */
    @GetMapping("/mceReceiveStatic")
    public ApiResult<Map<String, Object>> mceReceiveStatic() {
        return ApiResult.success(mceReceiveService.mceReceiveStatic());
    }

    /**
     * 消息中心分页接口
     * @return
     */
    @GetMapping("/getMceReceiveList")
    public ApiResult<CommonPage<MceReceiveResponseVO>> getMceReceiveList(@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
                                                                         @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
        return ApiResult.success(mceReceiveService.getMceReceiveResponseVOList(pageNumber, pageSize));
    }

    /**
     * 已读
     * @return
     */
    @PutMapping
    ApiResult<Void> updateMceReceive(@RequestBody MceReceive mceReceive) {
        mceReceiveService.updateMceReceive(mceReceive);
        return ApiResult.success();
    }

    /**
     * 消息接收接口
     * @return
     */
    @Log(title = "新增发布消息", businessType = BusinessType.INSERT)
    @PostMapping("/mceAdd")
    ApiResult<Void> add(@RequestBody String mceReceive) {
        mceReceiveService.add(mceReceive);
        return ApiResult.success();
    }

    /**
     * 消息发布-无需token
     * @return
     */
    @PostMapping("/addMceNew")
    ApiResult<Void> addNoToken(@RequestBody String mceNoToken) {
        mceReceiveService.addNoToken(mceNoToken);
        return ApiResult.success();
    }


    /**
     * 删除消息及内容，并清空所有的消息接收表数据
     */
    @Log(title = "删除已发布消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{contentId}")
    public ApiResult<Void> removeById(@PathVariable("contentId") Integer id) {
        mceReceiveService.removeById(id);
        return ApiResult.success();
    }

    /**
     * 删除一条消息
     **/
    @Log(title = "删除消息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public ApiResult<Void> deleteById(@PathVariable("id") Integer id) {
        boolean f = mceReceiveService.deleteById(id);
        if (f) {
            return ApiResult.success();
        } else {
            return ApiResult.error("删除失败");
        }
    }
}

