package com.ruoyi.usernotice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.BeanCopyUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.usernotice.exception.UserNoticeExceptionEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.usernotice.domain.bo.UserNoticeBo;
import com.ruoyi.usernotice.domain.vo.UserNoticeVo;
import com.ruoyi.usernotice.domain.UserNotice;
import com.ruoyi.usernotice.mapper.UserNoticeMapper;
import com.ruoyi.usernotice.service.IUserNoticeService;


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

/**
 * C端用户通知Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-21
 */
@RequiredArgsConstructor
@Service
public class UserNoticeServiceImpl implements IUserNoticeService {

    private final UserNoticeMapper baseMapper;

    /**
     * 查询C端用户通知分页
     *
     * @param bo C端用户通知
     * @return C端用户通知
     */
    @Override
    public TableDataInfo<UserNoticeVo> queryPageList(UserNoticeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<UserNotice> lqw = buildQueryWrapper(bo);
        Page<UserNoticeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询C端用户通知列表
     *
     * @param bo C端用户通知
     * @return C端用户通知
     */
    @Override
    public List<UserNoticeVo> queryList(UserNoticeBo bo) {
        LambdaQueryWrapper<UserNotice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<UserNotice> buildQueryWrapper(UserNoticeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<UserNotice> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, UserNotice::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, UserNotice::getStatus, bo.getStatus());
        lqw.eq(bo.getEntityId() != null, UserNotice::getEntityId, bo.getEntityId());
        lqw.eq(bo.getEntityType() != null, UserNotice::getEntityType, bo.getEntityType());
        lqw.orderByAsc(UserNotice::getStatus);
        lqw.orderByDesc(UserNotice::getCreateTime);
        return lqw;
    }

    /**
     * 查询C端用户通知
     *
     * @param id C端用户通知主键
     * @return C端用户通知
     */
    @Override
    public UserNoticeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 详情C端用户通知
     *
     * @param id C端用户通知主键
     * @return C端用户通知
     */
    @Override
    public UserNotice loadById(Long id, Boolean tw){
        UserNotice info = this.baseMapper.selectById(id);
        if(ObjectUtil.isEmpty(info) && tw){
            throw new ServiceException(UserNoticeExceptionEnum.UserNotice_IS_NOT_EXISTS);
        }
        return info;
    }

    /**
     * 新增C端用户通知
     *
     * @param bo C端用户通知
     * @return 结果
     */
    @Override
    public Boolean insertByBo(UserNoticeBo bo) {
        UserNotice add = BeanUtil.toBean(bo, UserNotice.class);
        validEntityBeforeSave(add);
        add.setStatus(false);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改C端用户通知
     *
     * @param bo C端用户通知
     * @return 结果
     */
    @Override
    public Boolean updateByBo(UserNoticeBo bo) {
        UserNotice userNotice = baseMapper.selectById(bo.getId());
        UserNotice update = BeanCopyUtils.copy(bo, userNotice);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除C端用户通知
     *
     * @param ids 需要删除的C端用户通知主键
     * @return 结果
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 获取用户消息数
     * @param userId
     * @param status
     * @return
     */
    @Override
    public Long queryUserCount(Long userId, Boolean status) {
        return this.baseMapper.selectCount(new LambdaQueryWrapper<UserNotice>()
            .eq(UserNotice::getUserId, userId)
            .eq(UserNotice::getStatus, status)
        );

    }

    @Override
    public Boolean setStatus(Long id) {
        UserNotice userNotice = loadById(id, true);
        userNotice.setStatus(true);
        baseMapper.updateById(userNotice);
        return true;
    }

    @Override
    public Boolean setAllStatus(Long userId) {
         baseMapper.update(null, Wrappers.lambdaUpdate(UserNotice.class).set(UserNotice::getStatus, true).eq(UserNotice::getUserId, userId));
         return true;
    }

}
