package com.scm.contract.web.message;

import com.scm.contract.model.message.MessageCenter;
import com.scm.contract.service.message.MessageCenterService;
import com.scm.contract.tools.HumpLineAdopter;
import com.set.direwolf.core.mybatis.Page;
import com.set.direwolf.core.web.OperationResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息中心
 *
 * @author zuo
 * @date 2024/6/7 11:09
 */

@Controller
@RequestMapping("${adminPath}/message/center")
public class MessageCenterController {

    private final MessageCenterService messageCenterService;

    @Autowired
    public MessageCenterController(MessageCenterService messageCenterService) {
        this.messageCenterService = messageCenterService;
    }

    @PostMapping("/list")
    public @ResponseBody OperationResult List() {
        return OperationResult.buildSuccessResult("获取消息中心信息成功", messageCenterService.findList(new MessageCenter()));
    }

    /**
     * 分页查询数据获取
     *
     * @param entity   参数实体
     * @param request  请求
     * @param response 响应
     * @return Page
     */
    @PostMapping("/page")
    public @ResponseBody OperationResult page(@RequestBody MessageCenter entity, HttpServletRequest request, HttpServletResponse response) {
        entity.setIsAlarm(true);
        Page<MessageCenter> page = Page.bootstrapPage(request, response);
        List<List<String>> orderList = entity.getOrder();
        String defaultOrder = HumpLineAdopter.humpToLine(orderList);
        if (StringUtils.isNotEmpty(defaultOrder)) {
            page.setOrderBy(defaultOrder);
        }
        return OperationResult.buildSuccessResult("获取消息中心分页查询成功", messageCenterService.findPage(page, entity));
    }
}
