package cn.axzo.msg.center.inside.notices.service.impl;

import cn.axzo.core.domain.PageResult;
import cn.axzo.msg.center.api.InsideMessageRecordApi;
import cn.axzo.msg.center.api.request.CmsMsgListReq;
import cn.axzo.msg.center.api.request.CmsMsgQueryReq;
import cn.axzo.msg.center.api.request.MessageRecordDeleteReq;
import cn.axzo.msg.center.api.request.MessageRecordReq;
import cn.axzo.msg.center.api.request.MessageStatisticsReq;
import cn.axzo.msg.center.api.request.QueryMessageRecordReq;
import cn.axzo.msg.center.api.request.UpdateReadReq;
import cn.axzo.msg.center.api.response.MessageNewRes;
import cn.axzo.msg.center.api.response.MessageRecordRes;
import cn.axzo.msg.center.common.exception.ServiceException;
import cn.axzo.msg.center.common.utils.BeanConvertUtils;
import cn.axzo.msg.center.dal.MessageRecordDao;
import cn.axzo.msg.center.domain.dto.UpdateReadDTO;
import cn.axzo.msg.center.inside.notices.service.MessageRecordService;
import cn.azxo.framework.common.model.CommonResponse;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class InsideMessageRecordApiImpl implements InsideMessageRecordApi {

    @Resource
    private MessageRecordService messageRecordService;

    @Resource
    private MessageRecordDao messageRecordDao;

    @Override
    public CommonResponse<PageResult<MessageNewRes>> listMsgInfo4Trade(CmsMsgListReq req) {
        CmsMsgQueryReq request=new CmsMsgQueryReq();
        BeanUtils.copyProperties(req,request);
        PageResult<MessageNewRes> pageResult = messageRecordService.pageMsgInfo4WechatAndTrade(
                request, req.getPersonId());
        return CommonResponse.success(pageResult);
    }

    @Override
    public CommonResponse<PageResult<MessageRecordRes>> queryMessageRecordList(MessageRecordReq request) {
        IPage<MessageRecordRes> queryPage = messageRecordService.queryMessageRecordList(request);
        PageResult<MessageRecordRes> result = new PageResult<>(queryPage.getRecords(), queryPage.getTotal());
        return CommonResponse.success(result);
    }

    @Override
    public CommonResponse<Boolean> changeHasRead(UpdateReadReq request) {
        if(request.getToId() == null
                && request.getFromId() == null
                && CollectionUtil.isEmpty(request.getMsgIdList())){
            throw new ServiceException("发送人id、接收人id、消息ID列表不能同时为空");
        }
        UpdateReadDTO updateReadDTO = BeanConvertUtils.copyBean(request, UpdateReadDTO.class);
        Boolean updateSuccess = messageRecordService.changeHasRead(updateReadDTO);
        return CommonResponse.success(updateSuccess);
    }

    @Override
    public CommonResponse<PageResult<MessageRecordRes>> queryUnReadList(QueryMessageRecordReq request) {
        IPage<MessageRecordRes> result=messageRecordService.queryUnReadList(request);
        return CommonResponse.success(new PageResult<>(result.getRecords(), result.getTotal()));
    }

    @Override
    public CommonResponse<PageResult<MessageRecordRes>> querySent(QueryMessageRecordReq request) {
        IPage<MessageRecordRes> result=messageRecordService.querySent(request);
        return CommonResponse.success(new PageResult<>(result.getRecords(), result.getTotal()));
    }

    @Override
    public CommonResponse<Integer> unReadStatistics(MessageStatisticsReq request) {
        return CommonResponse.success(messageRecordService.unReadStatistics(request));
    }

    @Override
    public CommonResponse<Boolean> deleteMessageRecords(MessageRecordDeleteReq request) {
        return CommonResponse.success(messageRecordService.deleteMessageRecords(request));
    }

    @Override
    public CommonResponse<Void> markReceived(List<Long> msgIdList) {
        messageRecordService.updateStateReceive(msgIdList);
        return CommonResponse.success();
    }
}
