package com.ruoyi.wgz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.bgt.mapper.BgtProjectRecruitMapper;
import com.ruoyi.bgt.service.IBgtProjectRecruitService;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.domain.Annex;
import com.ruoyi.common.service.IAnnexService;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.wgz.bo.req.WgzAppConfirmRegistrationReq;
import com.ruoyi.wgz.bo.req.WgzAppGetMessageListReq;
import com.ruoyi.wgz.bo.req.WgzAppLeaveHistoryListPageReq;
import com.ruoyi.wgz.bo.req.WgzAppReadUnreadReq;
import com.ruoyi.wgz.bo.res.WgzAppGetMessageListRes;
import com.ruoyi.wgz.bo.res.WgzAppMessageTypeStatisticsRes;
import com.ruoyi.wgz.bo.res.WgzAppRegistrationInformationRes;
import com.ruoyi.wgz.bo.rests.WgzAppMessageTypeStatisticsTwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.wgz.bo.WgzMessageQueryBo;
import com.ruoyi.wgz.domain.WgzMessage;
import com.ruoyi.wgz.mapper.WgzMessageMapper;
import com.ruoyi.wgz.service.IWgzMessageService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 消息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
public class WgzMessageServiceImpl extends ServicePlusImpl<WgzMessageMapper, WgzMessage> implements IWgzMessageService {

    @Autowired
    private ISysDictTypeService dictTypeService;

	@Autowired
	private IAnnexService annexService;

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

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

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

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

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

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

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

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

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

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

    @Override
    public WgzAppMessageTypeStatisticsRes userMessageTypeStatistics(Long id) {
        WgzAppMessageTypeStatisticsRes res = new WgzAppMessageTypeStatisticsRes();
        List<WgzAppMessageTypeStatisticsTwo> list = new ArrayList<>();
        //1、获取字典类型
        List<SysDictData> data = dictTypeService.selectDictDataByType("message_large_type");
        for (SysDictData datum : data) {
            WgzAppMessageTypeStatisticsTwo two = new WgzAppMessageTypeStatisticsTwo();
            //2、统计数据
            LambdaQueryWrapper<WgzMessage> wp = new LambdaQueryWrapper<WgzMessage>().
                    eq(WgzMessage::getRecipientId, id).
                    eq(WgzMessage::getRecipientType, "1").
                    eq(WgzMessage::getReadStatus, "0").
                    eq(WgzMessage::getMessageLargeType, datum.getDictValue());
            two.setType(datum.getDictValue());
            two.setNum(baseMapper.delete(wp));
            list.add(two);
        }
        res.setList(list);
        return res;
    }

	@Override
	public TableDataInfo<WgzAppGetMessageListRes> userGetMessageList(WgzAppGetMessageListReq req) {
		Long appUserId = SecurityUtils.getAppUserId();
		req.setRecipientId(appUserId);
		Page<WgzAppGetMessageListReq> queryDTOPage = new Page<>();
		queryDTOPage.setCurrent(req.getPageNum());
		queryDTOPage.setSize(req.getPageSize());
		return PageUtils.buildDataInfo(baseMapper.userGetMessageList(queryDTOPage));
	}


	@Override
	public WgzAppRegistrationInformationRes userRegistrationInformation(Long messageId) {
		Long appUserId = SecurityUtils.getAppUserId();
		//1、获取当前消息的招工ID
		WgzMessage byId = this.getById(messageId);
		//2、根据招工ID得到招工信息及附件
		WgzAppRegistrationInformationRes byRecruitIdData = baseMapper.findByRecruitIdData(byId.getId());
//		List<Annex> byUserIdAndRecruitId = annexService.findByUserIdAndRecruitId(appUserId, byId.getId(), new String[]{"1"});
//		byRecruitIdData.setAnnex(null);
		return byRecruitIdData;
	}

	@Override
	public Boolean userReadUnread(WgzAppReadUnreadReq req) {
		WgzMessage wgzMessage = new WgzMessage().setId(req.getMessageId()).setReadStatus("1");
		return baseMapper.updateById(wgzMessage) > 0;
	}
}
