package com.inkasso.factoring.system.message.controller;

import com.inkasso.adapter.exception.enums.ExceptionEnum;
import com.inkasso.adapter.web.LoginUserHolder;
import com.inkasso.auth.annotation.Log;
import com.inkasso.auth.interfaces.PermissionAndCheck;
import com.inkasso.adapter.page.PageResult;
import com.inkasso.factoring.system.message.enums.MessageTypeEnum;
import com.inkasso.factoring.system.message.model.dto.MessageDTO;
import com.inkasso.factoring.system.message.model.request.QueryMessageRequest;
import com.inkasso.factoring.system.message.service.MessageService;
import com.inkasso.adapter.exception.ServiceException;
import com.inkasso.adapter.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author 张超
 * @date  2023-08-18
 */
@RestController
@RequestMapping(path = "system")
//@Permissions(name="系统消息",code = "messageManage")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @GetMapping(path="pageMessage")
    public ResponseData  pageQuery(QueryMessageRequest queryMessageRequest){
        Long id = LoginUserHolder.get().getId();
        queryMessageRequest.setReceiveUserId(id);
        queryMessageRequest.setFiledName("create_time");
        queryMessageRequest.setTenantId(LoginUserHolder.get().getTenantId());
        queryMessageRequest.setAsc(false);
        queryMessageRequest.setMsgType(MessageTypeEnum.NOTICE_MESSAGE.getCode());
        PageResult<MessageDTO> pageResultResponseData = messageService.pageQueryList(queryMessageRequest);
        return ResponseData.success(pageResultResponseData);
    }




    @PutMapping(path = "message/{messageId}")
    //@PermissionAndCheck(name="更新系统消息状态",code="MessageController:update")
    public ResponseData edit(@PathVariable("messageId") Long messageId) {
        messageService.editMessage(messageId);
        return ResponseData.success();
    }



    @PutMapping(path = "message")
    @PermissionAndCheck(name="批量更新系统消息状态(一键已读)",code="MessageController:batchUpdate")
    public ResponseData batchEdit() {
        messageService.batchUpdateMessage();
        return ResponseData.success();
    }



    @GetMapping(path = "message/{id}")
    @PermissionAndCheck(name = "查询系统消息详细信息", code="MessageController:detail")
    public ResponseData getById(@PathVariable("id") Long id){
        if(id==null){
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        MessageDTO messageDTO = messageService.getById(id);
        return ResponseData.success(messageDTO);
    }


    @DeleteMapping(path = "message/{id}")
    @PermissionAndCheck(name = "删除系统消息", code="MessageController:delete")
    public ResponseData deleteById(@PathVariable("id") Long id){
        if(id==null){
            throw new ServiceException(400, ExceptionEnum.SYSTEM_DATE_ERROR);
        }
        messageService.deleteMessage(id);
        return ResponseData.success();
    }
    @PostMapping(path="queryNum")
    @Log("车队长")
    public ResponseData queryMessageNum(@RequestBody QueryMessageRequest queryMessageRequest){
        Object o = messageService.queryMessageNum(queryMessageRequest);
        return ResponseData.success(o);
    }

}