package com.ningdatech.pmapi.sys.manage;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ningdatech.basic.function.VUtils;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.sys.model.entity.Notify;
import com.ningdatech.pmapi.sys.model.req.NotifyListReq;
import com.ningdatech.pmapi.sys.model.vo.NotifyVO;
import com.ningdatech.pmapi.sys.service.INotifyService;
import com.ningdatech.pmapi.todocenter.constant.WorkNoticeConstant;
import com.ningdatech.pmapi.user.util.LoginUserUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * NotifyManage
 * </p>
 *
 * @author ZPF
 * @since 21:30 2023/03/21
 */
@Component
@RequiredArgsConstructor
public class NotifyManage {

    private final INotifyService notifyService;

    public PageVo<NotifyVO> page(NotifyListReq notifyListReq) {
        Long userId = LoginUserUtil.getUserId();
        Page<Notify> page = notifyListReq.page();
        LambdaQueryWrapper<Notify> wrapper = Wrappers.lambdaQuery(Notify.class)
                .eq(Objects.nonNull(notifyListReq.getReaded()), Notify::getReaded, notifyListReq.getReaded())
                .eq(Notify::getUserId, userId)
                .like(Objects.nonNull(notifyListReq.getTitle()), Notify::getTitle, notifyListReq.getTitle())
                .eq(Objects.nonNull(notifyListReq.getType()), Notify::getType, notifyListReq.getType())
                .orderByDesc(Notify::getCreateTime);
        notifyService.page(page, wrapper);
        if (page.getTotal() == 0L) {
            return PageVo.empty();
        }
        List<NotifyVO> list = page.getRecords().stream()
                .map(n -> {
                    NotifyVO notifyVo = new NotifyVO();
                    BeanUtil.copyProperties(n, notifyVo);
                    String extraInfo = n.getExtraInfo();
                    if (StringUtils.isNotBlank(extraInfo)) {
                        JSONObject jsonObject = JSON.parseObject(extraInfo);
                        Long projectId = jsonObject.getLong(WorkNoticeConstant.PROJECT_ID);
                        String instanceId = jsonObject.getString(WorkNoticeConstant.INSTANCE_ID);
                        Long meetingId = jsonObject.getLong(WorkNoticeConstant.MEETING_ID);
                        notifyVo.setProjectId(projectId);
                        notifyVo.setInstanceId(instanceId);
                        notifyVo.setMeetingId(meetingId);
                    }
                    return notifyVo;
                })
                .collect(Collectors.toList());
        return PageVo.of(list, page.getTotal());
    }

    public NotifyVO detail(Long id) {
        Notify one = notifyService.getById(id);
        return BeanUtil.copyProperties(one, NotifyVO.class);
    }

    public Boolean read(Long id) {
        Notify one = notifyService.getById(id);
        VUtils.isTrue(Objects.isNull(one)).throwMessage("该通知不存在");
        one.setReaded(Boolean.TRUE);
        return notifyService.updateById(one);
    }
}
