package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.ZsMessagesBo;
import com.ruoyi.system.domain.vo.ZsMessagesVo;
import com.ruoyi.system.domain.ZsMessages;
import com.ruoyi.system.mapper.ZsMessagesMapper;
import com.ruoyi.system.service.IZsMessagesService;

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

/**
 * 站点留言Service业务层处理
 *
 * @author 王强
 * @date 2025-05-06
 */
@RequiredArgsConstructor
@Service
public class ZsMessagesServiceImpl implements IZsMessagesService {

    private final ZsMessagesMapper baseMapper;

    /**
     * 查询站点留言
     */
    @Override
    public ZsMessagesVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询站点留言列表
     */
    @Override
    public TableDataInfo<ZsMessagesVo> queryPageList(ZsMessagesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZsMessages> lqw = buildQueryWrapper(bo);
        Page<ZsMessagesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询站点留言列表
     */
    @Override
    public List<ZsMessagesVo> queryList(ZsMessagesBo bo) {
        LambdaQueryWrapper<ZsMessages> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZsMessages> buildQueryWrapper(ZsMessagesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsMessages> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginCreatedAt") != null && params.get("endCreatedAt") != null,
            ZsMessages::getCreatedAt ,params.get("beginCreatedAt"), params.get("endCreatedAt"));
        lqw.eq(bo.getStatus() != null, ZsMessages::getStatus, bo.getStatus());
        lqw.between(params.get("beginAdminReplyAt") != null && params.get("endAdminReplyAt") != null,
            ZsMessages::getAdminReplyAt ,params.get("beginAdminReplyAt"), params.get("endAdminReplyAt"));
        return lqw;
    }

    /**
     * 新增站点留言
     */
    @Override
    public Boolean insertByBo(ZsMessagesBo bo) {
        ZsMessages add = BeanUtil.toBean(bo, ZsMessages.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改站点留言
     */
    @Override
    public Boolean updateByBo(ZsMessagesBo bo) {
        ZsMessages update = BeanUtil.toBean(bo, ZsMessages.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZsMessages entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除站点留言
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
