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 com.ruoyi.oa.domain.OaProgressDetail;
import com.ruoyi.oa.domain.bo.OaProgressDetailBo;
import com.ruoyi.oa.service.IOaProgressDetailService;
import com.ruoyi.oa.service.ISysOaProjectService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.oa.domain.bo.OaProgressBo;
import com.ruoyi.oa.domain.vo.OaProgressVo;
import com.ruoyi.oa.domain.OaProgress;
import com.ruoyi.oa.mapper.OaProgressMapper;
import com.ruoyi.oa.service.IOaProgressService;

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

/**
 * 进度管理Service业务层处理
 *
 * @author hdka
 * @date 2025-04-14
 */
@RequiredArgsConstructor
@Service
public class OaProgressServiceImpl implements IOaProgressService {

    private final OaProgressMapper baseMapper;

    private final IOaProgressDetailService detailService;

    private final ISysOaProjectService projectService;

    /**
     * 查询进度管理
     */
    @Override
    public OaProgressVo queryById(Long progressId){
        return baseMapper.selectVoById(progressId);
    }

    /**
     * 查询进度管理列表
     */
    @Override
    public TableDataInfo<OaProgressVo> queryPageList(OaProgressBo bo, PageQuery pageQuery) {
        QueryWrapper<OaProgress> lqw = buildQueryWrapper(bo);
        Page<OaProgressVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询进度管理列表
     */
    @Override
    public List<OaProgressVo> queryList(OaProgressBo bo) {
        QueryWrapper<OaProgress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaProgress> buildQueryWrapper(OaProgressBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaProgress> lqw = Wrappers.query();
        lqw.eq("op.del_flag", 0);
        lqw.eq(bo.getProjectId() != null, "op.project_id", bo.getProjectId());
        lqw.eq(bo.getType() != null, "op.type", bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getProgressName()), "op.progress_name", bo.getProgressName());
        lqw.eq(bo.getParentId() != null, "op.parent_id", bo.getParentId());
        lqw.eq(bo.getStatus() != null, "op.status", bo.getStatus());
        return lqw;
    }

    /**
     * 新增进度管理
     */
    @Override
    public Boolean insertByBo(OaProgressBo bo) {
        OaProgress add = BeanUtil.toBean(bo, OaProgress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if(bo.getDetailList() != null && !bo.getDetailList().isEmpty()) {
            for (OaProgressDetailBo detailBo : bo.getDetailList()) {
                detailBo.setProgressId(add.getProgressId());
                detailService.insertByBo(detailBo);
            }
        }

        return flag;
    }

    /**
     * 修改进度管理
     */
    @Override
    public Boolean updateByBo(OaProgressBo bo) {
        OaProgress update = BeanUtil.toBean(bo, OaProgress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除进度管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        for (Long id : ids) {
            QueryWrapper<OaProgress> oaProgressQueryWrapper = new QueryWrapper<>();
            oaProgressQueryWrapper.eq("parent_id", id);
            baseMapper.delete(oaProgressQueryWrapper);
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<OaProgressDetail> getKeyList() {
        return baseMapper.getKeyList();
    }

    @Override
    public List<OaProgressVo> queryByProjectId(Long projectId) {

        return baseMapper.queryByProjectId(projectId);
    }
}
