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 org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaExpressQuestionBo;
import com.ruoyi.oa.domain.vo.OaExpressQuestionVo;
import com.ruoyi.oa.domain.OaExpressQuestion;
import com.ruoyi.oa.mapper.OaExpressQuestionMapper;
import com.ruoyi.oa.service.IOaExpressQuestionService;

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

/**
 * 快递问题Service业务层处理
 *
 * @author hdka
 * @date 2025-07-21
 */
@RequiredArgsConstructor
@Service
public class OaExpressQuestionServiceImpl implements IOaExpressQuestionService {

    private final OaExpressQuestionMapper baseMapper;

    /**
     * 查询快递问题
     */
    @Override
    public OaExpressQuestionVo queryById(Long questionId){
        return baseMapper.selectVoById(questionId);
    }

    /**
     * 查询快递问题列表
     */
    @Override
    public TableDataInfo<OaExpressQuestionVo> queryPageList(OaExpressQuestionBo bo, PageQuery pageQuery) {
        QueryWrapper<OaExpressQuestion> lqw = buildQueryWrapperPlus(bo);
        Page<OaExpressQuestionVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询快递问题列表
     */
    @Override
    public List<OaExpressQuestionVo> queryList(OaExpressQuestionBo bo) {
        LambdaQueryWrapper<OaExpressQuestion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
    private LambdaQueryWrapper<OaExpressQuestion> buildQueryWrapper(OaExpressQuestionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaExpressQuestion> lqw = new LambdaQueryWrapper<>();

        if (bo.getExpressId() != null) {
            lqw.eq(OaExpressQuestion::getExpressId, bo.getExpressId());
        }
        if (StringUtils.isNotBlank(bo.getDescription())) {
            lqw.eq(OaExpressQuestion::getDescription, bo.getDescription());
        }
        if (bo.getReportTime() != null) {
            lqw.eq(OaExpressQuestion::getReportTime, bo.getReportTime());
        }
        if (StringUtils.isNotBlank(bo.getReportBy())) {
            lqw.eq(OaExpressQuestion::getReportBy, bo.getReportBy());
        }
        if (bo.getStatus() != null) {
            lqw.eq(OaExpressQuestion::getStatus, bo.getStatus());
        }
        return lqw;
    }

    private QueryWrapper<OaExpressQuestion> buildQueryWrapperPlus(OaExpressQuestionBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaExpressQuestion> qw = new QueryWrapper<>();
        // 设置表别名
        if (bo.getExpressId() != null) {
            qw.eq("oeq.express_id", bo.getExpressId());
        }
        if (StringUtils.isNotBlank(bo.getDescription())) {
            qw.eq("oeq.description", bo.getDescription());
        }
        if (bo.getReportTime() != null) {
            qw.eq("oeq.report_time", bo.getReportTime());
        }
        if (StringUtils.isNotBlank(bo.getReportBy())) {
            qw.eq("oeq.report_by", bo.getReportBy());
        }
        if (bo.getStatus() != null) {
            qw.eq("oeq.status", bo.getStatus());
        }
        // 逻辑删除
        qw.eq("oeq.del_flag", 0);

        return qw;
    }

    /**
     * 新增快递问题
     */
    @Override
    public Boolean insertByBo(OaExpressQuestionBo bo) {
        OaExpressQuestion add = BeanUtil.toBean(bo, OaExpressQuestion.class);
        validEntityBeforeSave(add);
        add.setReportBy(LoginHelper.getNickName());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQuestionId(add.getQuestionId());
        }
        return flag;
    }

    /**
     * 修改快递问题
     */
    @Override
    public Boolean updateByBo(OaExpressQuestionBo bo) {
        OaExpressQuestion update = BeanUtil.toBean(bo, OaExpressQuestion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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