package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.helper.LoginHelper;
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 lombok.val;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaFeedbackBo;
import com.ruoyi.oa.domain.vo.OaFeedbackVo;
import com.ruoyi.oa.domain.OaFeedback;
import com.ruoyi.oa.mapper.OaFeedbackMapper;
import com.ruoyi.oa.service.IOaFeedbackService;

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

/**
 * 问题反馈Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class OaFeedbackServiceImpl implements IOaFeedbackService {

    private final OaFeedbackMapper baseMapper;

    /**
     * 查询问题反馈
     */
    @Override
    public OaFeedbackVo queryById(Long feedbackId){
        return baseMapper.selectVoById(feedbackId);
    }

    /**
     * 查询问题反馈列表
     */
    @Override
    public TableDataInfo<OaFeedbackVo> queryPageList(OaFeedbackBo bo, PageQuery pageQuery) {
        QueryWrapper<OaFeedback> lqw = buildQueryWrapper(bo);
        val result = baseMapper.selectVoPageAndStatus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 查询问题反馈列表
     */
    @Override
    public TableDataInfo<OaFeedbackVo> indexQueryList(OaFeedbackBo bo, PageQuery pageQuery) {
        QueryWrapper<OaFeedback> lqw = buildQueryWrapper(bo);
        lqw.eq("ofi.status", 0L);
        val result = baseMapper.selectVoPageAndStatus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询问题反馈列表
     */
    @Override
    public List<OaFeedbackVo> queryList(OaFeedbackBo bo) {
        QueryWrapper<OaFeedback> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaFeedback> buildQueryWrapper(OaFeedbackBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaFeedback> lqw = Wrappers.query();
        lqw.eq("ofi.user_id", LoginHelper.getUserId());
        return lqw;
    }

    /**
     * 新增问题反馈
     */
    @Override
    public Boolean insertByBo(OaFeedbackBo bo) {

        OaFeedback add = BeanUtil.toBean(bo, OaFeedback.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFeedbackId(add.getFeedbackId());
        }
        baseMapper.insertItem(add.getFeedbackId());
        return flag;
    }

    /**
     * 修改问题反馈
     */
    @Override
    public Boolean updateByBo(OaFeedbackBo bo) {
        OaFeedback update = BeanUtil.toBean(bo, OaFeedback.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean updateToRead(OaFeedbackBo bo) {

        return baseMapper.updateToRead(bo.getFeedbackId(),LoginHelper.getUserId())>0;
    }

    @Override
    public int delItem(Long feedbackId) {

        return baseMapper.delItem(feedbackId,LoginHelper.getUserId());
    }
}
