package com.hotwj.platform.operationManagement.trainingFeedbackReply.service.impl;

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.hotwj.platform.operationManagement.trainingFeedbackReply.domain.HotTrainingFeedbackReply;
import com.hotwj.platform.operationManagement.trainingFeedbackReply.domain.bo.HotTrainingFeedbackReplyBo;
import com.hotwj.platform.operationManagement.trainingFeedbackReply.domain.vo.HotTrainingFeedbackReplyVo;
import com.hotwj.platform.operationManagement.trainingFeedbackReply.mapper.HotTrainingFeedbackReplyMapper;
import com.hotwj.platform.operationManagement.trainingFeedbackReply.service.IHotTrainingFeedbackReplyService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 培训反馈回复Service业务层处理
 *
 * @author shihongwei
 * @date 2026-05-07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotTrainingFeedbackReplyServiceImpl implements IHotTrainingFeedbackReplyService {

    private final HotTrainingFeedbackReplyMapper baseMapper;

    /**
     * 查询培训反馈回复
     *
     * @param id 主键
     * @return 培训反馈回复
     */
    @Override
    public HotTrainingFeedbackReplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询培训反馈回复列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 培训反馈回复分页列表
     */
    @Override
    public TableDataInfo<HotTrainingFeedbackReplyVo> queryPageList(HotTrainingFeedbackReplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotTrainingFeedbackReply> lqw = buildQueryWrapper(bo);
        Page<HotTrainingFeedbackReplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的培训反馈回复列表
     *
     * @param bo 查询条件
     * @return 培训反馈回复列表
     */
    @Override
    public List<HotTrainingFeedbackReplyVo> queryList(HotTrainingFeedbackReplyBo bo) {
        LambdaQueryWrapper<HotTrainingFeedbackReply> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotTrainingFeedbackReply> buildQueryWrapper(HotTrainingFeedbackReplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotTrainingFeedbackReply> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotTrainingFeedbackReply::getId);
        lqw.eq(bo.getCompanyId() != null, HotTrainingFeedbackReply::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getFeedbackId() != null, HotTrainingFeedbackReply::getFeedbackId, bo.getFeedbackId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackNo()), HotTrainingFeedbackReply::getFeedbackNo, bo.getFeedbackNo());
        lqw.eq(bo.getReplyRole() != null, HotTrainingFeedbackReply::getReplyRole, bo.getReplyRole());
        lqw.eq(bo.getReplyUserId() != null, HotTrainingFeedbackReply::getReplyUserId, bo.getReplyUserId());
        lqw.like(StringUtils.isNotBlank(bo.getReplyUserName()), HotTrainingFeedbackReply::getReplyUserName, bo.getReplyUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getReplyContent()), HotTrainingFeedbackReply::getReplyContent, bo.getReplyContent());
        lqw.eq(bo.getReplyTime() != null, HotTrainingFeedbackReply::getReplyTime, bo.getReplyTime());
        lqw.eq(bo.getStatus() != null, HotTrainingFeedbackReply::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotTrainingFeedbackReply::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotTrainingFeedbackReply::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotTrainingFeedbackReply::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增培训反馈回复
     *
     * @param bo 培训反馈回复
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotTrainingFeedbackReplyBo bo) {
        HotTrainingFeedbackReply add = MapstructUtils.convert(bo, HotTrainingFeedbackReply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改培训反馈回复
     *
     * @param bo 培训反馈回复
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotTrainingFeedbackReplyBo bo) {
        HotTrainingFeedbackReply update = MapstructUtils.convert(bo, HotTrainingFeedbackReply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除培训反馈回复信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
