package com.ruoyi.bgt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
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.ruoyi.bgt.bo.BgtMessageQueryBo;
import com.ruoyi.bgt.domain.BgtMessage;
import com.ruoyi.bgt.domain.dto.BgtMessageMyListDTO;
import com.ruoyi.bgt.domain.dto.BgtMessageOtherDTO;
import com.ruoyi.bgt.domain.dto.BgtProjectRecruitApplyQueryDTO;
import com.ruoyi.bgt.domain.dto.BgtProjectRecruitQueryDTO;
import com.ruoyi.bgt.domain.vo.BgtMessageCountVO;
import com.ruoyi.bgt.domain.vo.BgtMessageOtherVO;
import com.ruoyi.bgt.domain.vo.BgtMessageVO;
import com.ruoyi.bgt.domain.vo.BgtProjectRecruitApplyVO;
import com.ruoyi.bgt.mapper.BgtMessageMapper;
import com.ruoyi.bgt.service.IBgtMessageService;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BgtMessageType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 消息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-25
 */
@Service
public class BgtMessageServiceImpl extends ServicePlusImpl<BgtMessageMapper, BgtMessage> implements IBgtMessageService {

    @Override
    public BgtMessage queryById(Long id){
        return getById(id);
    }

    @Override
    public TableDataInfo<BgtMessage> queryPageList(BgtMessageQueryBo bo) {
        Page<BgtMessage> result = page(PageUtils.buildPage(), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<BgtMessage> queryList(BgtMessageQueryBo bo) {
        return list(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<BgtMessage> buildQueryWrapper(BgtMessageQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BgtMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getSenderType()), BgtMessage::getSenderType, bo.getSenderType());
        lqw.eq(bo.getSenderId() != null, BgtMessage::getSenderId, bo.getSenderId());
        lqw.eq(StrUtil.isNotBlank(bo.getRecipientType()), BgtMessage::getRecipientType, bo.getRecipientType());
        lqw.eq(bo.getRecipientId() != null, BgtMessage::getRecipientId, bo.getRecipientId());
        lqw.eq(StrUtil.isNotBlank(bo.getHeadline()), BgtMessage::getHeadline, bo.getHeadline());
        lqw.eq(StrUtil.isNotBlank(bo.getSubheading()), BgtMessage::getSubheading, bo.getSubheading());
        lqw.eq(bo.getTableId() != null, BgtMessage::getTableId, bo.getTableId());
        lqw.like(StrUtil.isNotBlank(bo.getTableName()), BgtMessage::getTableName, bo.getTableName());
        lqw.eq(StrUtil.isNotBlank(bo.getMessageLargeType()), BgtMessage::getMessageLargeType, bo.getMessageLargeType());
        lqw.eq(StrUtil.isNotBlank(bo.getMessageSmallType()), BgtMessage::getMessageSmallType, bo.getMessageSmallType());
        lqw.eq(StrUtil.isNotBlank(bo.getReadStatus()), BgtMessage::getReadStatus, bo.getReadStatus());
        return lqw;
    }

    @Override
    public Boolean insert(BgtMessage bo) {
        BgtMessage add = BeanUtil.toBean(bo, BgtMessage.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean update(BgtMessage bo) {
        BgtMessage update = BeanUtil.toBean(bo, BgtMessage.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BgtMessage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	/**
	 * LC-APP相关
	 * =================================================================================================================
	 * =================================================================================================================
	 * =================================================================================================================
	 */

	@Override
	public Boolean sendAMessage(BgtMessage bo) {
		return save(bo);
	}

	@Override
	public BgtMessageCountVO countUnread() {
		List<BgtMessage> bgtMessages = baseMapper.selectList(Wrappers.<BgtMessage>lambdaQuery()
			.eq(BgtMessage::getRecipientId, SecurityUtils.getAppUserId())
			.eq(BgtMessage::getReadStatus, "0"));
		Map<String, Long> map = bgtMessages.stream().collect(Collectors.groupingBy(BgtMessage::getMessageLargeType, Collectors.counting()));

		BgtMessageCountVO bgtMessageCountVO = new BgtMessageCountVO();
		bgtMessageCountVO.setTaskMessageCount(map.getOrDefault(BgtMessageType.TASK.getCode(),0L).intValue());
		bgtMessageCountVO.setSettlementMessageCount(map.getOrDefault(BgtMessageType.SETTLEMENT.getCode(),0L).intValue());
		bgtMessageCountVO.setOtherMessageCount(map.getOrDefault(BgtMessageType.OTHER.getCode(),0L).intValue());
		return bgtMessageCountVO;
	}

	@Override
	public TableDataInfo<BgtMessageVO> queryMyPageList(BgtMessageMyListDTO dto) {
		LambdaQueryWrapper<BgtMessage> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(BgtMessage::getRecipientId, SecurityUtils.getAppUserId());
		Page<BgtMessage> result = page(PageUtils.buildPage(), wrapper);
		Page<BgtMessageVO> bgtMessageVOPage = new Page<>();
		bgtMessageVOPage.setTotal(result.getTotal());
		bgtMessageVOPage.setRecords(BeanUtil.copyToList(result.getRecords(),BgtMessageVO.class));
		return PageUtils.buildDataInfo(bgtMessageVOPage);
	}

	@Override
	public TableDataInfo<BgtMessageOtherVO> queryOtherPageList(BgtMessageOtherDTO dto) {
		Page<BgtMessageOtherDTO> queryDTOPage = new Page<>();
		queryDTOPage.setCurrent(dto.getPageNum());
		queryDTOPage.setSize(dto.getPageSize());
		Page<BgtMessageOtherVO> queryVOPage = baseMapper.queryOtherPageList(queryDTOPage, dto);
		return PageUtils.buildDataInfo(queryVOPage);
	}
}
