package com.fufan.mtsaas.modules.base.sysmsg.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.constant.DefaultConstant;
import com.fufan.mtsaas.modules.base.sysmsg.bo.request.MsgQueryRequest;
import com.fufan.mtsaas.modules.base.sysmsg.bo.request.MsgSaveRequest;
import com.fufan.mtsaas.modules.base.sysmsg.bo.response.MsgResponse;
import com.fufan.mtsaas.modules.base.sysmsg.dao.TSysMessageDao;
import com.fufan.mtsaas.modules.base.sysmsg.model.TSysMessage;
import com.fufan.mtsaas.modules.base.sysmsg.service.TSysMessageService;
import com.fufan.mtsaas.modules.common.bo.request.DeleteRequest;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

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

/**
 * 系统消息表(TSysMessage)表服务实现类
 *
 * @author Liby
 * @since 2025-01-08 15:58:35
 */
@Service("tSysMessageServiceImpl")
@Primary
@Slf4j
public class TSysMessageServiceImpl extends ServiceImpl<TSysMessageDao, TSysMessage> implements TSysMessageService {
    @Resource
    private TSysMessageDao tSysMessageDao;

    @Override
    public Result doQueryPage(MsgQueryRequest map) {
        Result<MsgResponse> result = new Result<>();
        Page<MsgResponse> page = new Page<>();

        // 当前用户ID
        RequestPojo requestPojo = ParamUtil.doChange();
        String staffId = requestPojo.getSysId();
        map.setOperator(staffId);

        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                tSysMessageDao.selectByPage(page, map);
            }
        });

        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        //分页查询
        List<MsgResponse> list = tSysMessageDao.selectByPage(page, map);

        page.setRecords(list);
        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }

    @Override
    public Result doSave(MsgSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doRead(MsgSaveRequest entity, RequestPojo requestPojo) {
        TSysMessage tSysMessage = tSysMessageDao.selectById(entity.getId());
        if (null != tSysMessage) {
            tSysMessage.setOperateStatus(DefaultConstant.NUM_1);
            tSysMessageDao.updateById(tSysMessage);
        }
        Result result = new Result<>();
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        return result;
    }

    @Override
    public Result doUnReadNum(RequestPojo requestPojo) {
        Result<MsgResponse> result = new Result<>();
        Page<MsgResponse> page = new Page<>();
        MsgQueryRequest map = new MsgQueryRequest();
        // 当前用户ID
        String staffId = requestPojo.getStaffId();
        map.setOperator(staffId);
        map.setOperateStatus(DefaultConstant.NUM_0);

        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                tSysMessageDao.selectByPage(page, map);
            }
        });

        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }


    @Override
    public Result doUpdate(MsgSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doDelete(DeleteRequest map, RequestPojo requestPojo) {
        return null;
    }
}
