package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.OaProgressDetailBo;
import com.ruoyi.oa.domain.vo.OaProgressDetailVo;
import com.ruoyi.oa.domain.OaProgressDetail;
import com.ruoyi.oa.mapper.OaProgressDetailMapper;
import com.ruoyi.oa.service.IOaProgressDetailService;

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

/**
 * 进度扩展Service业务层处理
 *
 * @author hdka2
 * @date 2025-04-14
 */
@RequiredArgsConstructor
@Service
public class OaProgressDetailServiceImpl implements IOaProgressDetailService {

    private final OaProgressDetailMapper baseMapper;

    /**
     * 查询进度扩展
     */
    @Override
    public OaProgressDetailVo queryById(Long detailId){
        return baseMapper.selectVoById(detailId);
    }

    /**
     * 查询进度扩展列表
     */
    @Override
    public TableDataInfo<OaProgressDetailVo> queryPageList(OaProgressDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaProgressDetail> lqw = buildQueryWrapper(bo);
        Page<OaProgressDetailVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询进度扩展列表
     */
    @Override
    public List<OaProgressDetailVo> queryList(OaProgressDetailBo bo) {
        LambdaQueryWrapper<OaProgressDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaProgressDetail> buildQueryWrapper(OaProgressDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaProgressDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(OaProgressDetail::getDelFlag,0L);
        lqw.eq(bo.getProgressId() != null, OaProgressDetail::getProgressId, bo.getProgressId());
        lqw.like(StringUtils.isNotBlank(bo.getDetailName()), OaProgressDetail::getDetailName, bo.getDetailName());
        lqw.eq(bo.getPlanStartDate() != null, OaProgressDetail::getPlanStartDate, bo.getPlanStartDate());
        lqw.eq(bo.getPlanEndDate() != null, OaProgressDetail::getPlanEndDate, bo.getPlanEndDate());
        lqw.eq(bo.getActualStartDate() != null, OaProgressDetail::getActualStartDate, bo.getActualStartDate());
        lqw.eq(bo.getActualEndDate() != null, OaProgressDetail::getActualEndDate, bo.getActualEndDate());
        lqw.eq(bo.getCompletePercent() != null, OaProgressDetail::getCompletePercent, bo.getCompletePercent());
        lqw.eq(bo.getPlanPayDate() != null, OaProgressDetail::getPlanPayDate, bo.getPlanPayDate());
        lqw.eq(bo.getPayAmount() != null, OaProgressDetail::getPayAmount, bo.getPayAmount());
        lqw.eq(bo.getPaidAmount() != null, OaProgressDetail::getPaidAmount, bo.getPaidAmount());
        lqw.eq(bo.getDetailStatus() != null, OaProgressDetail::getDetailStatus, bo.getDetailStatus());
        return lqw;
    }

    /**
     * 新增进度扩展
     */
    @Override
    public Boolean insertByBo(OaProgressDetailBo bo) {
        OaProgressDetail add = BeanUtil.toBean(bo, OaProgressDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setDetailId(add.getDetailId());
        }
        return flag;
    }

    /**
     * 修改进度扩展
     */
    @Override
    public Boolean updateByBo(OaProgressDetailBo bo) {
        OaProgressDetail update = BeanUtil.toBean(bo, OaProgressDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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