package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.OaRequirementsBo;
import com.ruoyi.oa.domain.vo.OaRequirementsVo;
import com.ruoyi.oa.domain.OaRequirements;
import com.ruoyi.oa.mapper.OaRequirementsMapper;
import com.ruoyi.oa.service.IOaRequirementsService;

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

/**
 * OA 需求Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-27
 */
@RequiredArgsConstructor
@Service
public class OaRequirementsServiceImpl implements IOaRequirementsService {

    private final OaRequirementsMapper baseMapper;

    /**
     * 查询OA 需求
     */
    @Override
    public OaRequirementsVo queryById(Long requirementId){
        return baseMapper.selectVoById(requirementId);
    }

    /**
     * 查询OA 需求列表
     */
    @Override
    public TableDataInfo<OaRequirementsVo> queryPageList(OaRequirementsBo bo, PageQuery pageQuery) {
        QueryWrapper<OaRequirements> lqw = buildQueryWrapper(bo);
        Page<OaRequirementsVo> result = baseMapper.selectVoListPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询OA 需求列表
     */
    @Override
    public List<OaRequirementsVo> queryList(OaRequirementsBo bo) {
        QueryWrapper<OaRequirements> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoListForExport(lqw);
    }

    private QueryWrapper<OaRequirements> buildQueryWrapper(OaRequirementsBo bo) {
        QueryWrapper<OaRequirements> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(bo.getTitle()), "r.title", bo.getTitle());
        qw.eq(bo.getRequesterId() != null, "r.requester_id", bo.getRequesterId());
        qw.eq(bo.getOwnerId() != null, "r.owner_id", bo.getOwnerId());
        qw.eq(bo.getProjectId() != null, "r.project_id", bo.getProjectId());
        qw.like(StringUtils.isNotBlank(bo.getDescription()), "r.description", bo.getDescription());
        qw.eq(bo.getDeadline() != null, "r.deadline", bo.getDeadline());
        qw.eq(bo.getStatus() != null, "r.status", bo.getStatus());
        qw.eq(StringUtils.isNotBlank(bo.getAccessory()), "r.accessory", bo.getAccessory());
        qw.eq("r.del_flag", 0);
        //根据创建时间倒叙
        qw.orderByDesc("r.create_time");
        return qw;
    }

    /**
     * 新增OA 需求
     */
    @Override
    public Boolean insertByBo(OaRequirementsBo bo) {
        OaRequirements add = BeanUtil.toBean(bo, OaRequirements.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRequirementId(add.getRequirementId());
        }
        return flag;
    }

    /**
     * 修改OA 需求
     */
    @Override
    public Boolean updateByBo(OaRequirementsBo bo) {
        OaRequirements update = BeanUtil.toBean(bo, OaRequirements.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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