package com.hotwj.platform.operationManagement.trainingFeedback.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.trainingFeedback.domain.HotTrainingFeedback;
import com.hotwj.platform.operationManagement.trainingFeedback.domain.bo.HotTrainingFeedbackBo;
import com.hotwj.platform.operationManagement.trainingFeedback.domain.vo.HotTrainingFeedbackVo;
import com.hotwj.platform.operationManagement.trainingFeedback.mapper.HotTrainingFeedbackMapper;
import com.hotwj.platform.operationManagement.trainingFeedback.service.IHotTrainingFeedbackService;
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 HotTrainingFeedbackServiceImpl implements IHotTrainingFeedbackService {

    private final HotTrainingFeedbackMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<HotTrainingFeedback> buildQueryWrapper(HotTrainingFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotTrainingFeedback> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotTrainingFeedback::getId);
        lqw.eq(bo.getCompanyId() != null, HotTrainingFeedback::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackNo()), HotTrainingFeedback::getFeedbackNo, bo.getFeedbackNo());
        lqw.eq(bo.getFeedbackUserId() != null, HotTrainingFeedback::getFeedbackUserId, bo.getFeedbackUserId());
        lqw.like(StringUtils.isNotBlank(bo.getFeedbackUserName()), HotTrainingFeedback::getFeedbackUserName, bo.getFeedbackUserName());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackKeyword()), HotTrainingFeedback::getFeedbackKeyword, bo.getFeedbackKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getFeedbackContent()), HotTrainingFeedback::getFeedbackContent, bo.getFeedbackContent());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachmentUrls()), HotTrainingFeedback::getAttachmentUrls, bo.getAttachmentUrls());
        lqw.eq(bo.getFeedbackTime() != null, HotTrainingFeedback::getFeedbackTime, bo.getFeedbackTime());
        lqw.eq(bo.getStatus() != null, HotTrainingFeedback::getStatus, bo.getStatus());
        lqw.eq(bo.getReplyTime() != null, HotTrainingFeedback::getReplyTime, bo.getReplyTime());
        lqw.eq(bo.getReplyCount() != null, HotTrainingFeedback::getReplyCount, bo.getReplyCount());
        lqw.eq(StringUtils.isNotBlank(bo.getLastReplyContent()), HotTrainingFeedback::getLastReplyContent, bo.getLastReplyContent());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotTrainingFeedback::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotTrainingFeedback::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotTrainingFeedback::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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