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.OaPaymentProgressBo;
import com.ruoyi.oa.domain.vo.OaPaymentProgressVo;
import com.ruoyi.oa.domain.OaPaymentProgress;
import com.ruoyi.oa.mapper.OaPaymentProgressMapper;
import com.ruoyi.oa.service.IOaPaymentProgressService;

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

/**
 * 项目付款进度Service业务层处理
 *
 * @author hdka
 * @date 2025-05-23
 */
@RequiredArgsConstructor
@Service
public class OaPaymentProgressServiceImpl implements IOaPaymentProgressService {

    private final OaPaymentProgressMapper baseMapper;

    /**
     * 查询项目付款进度
     */
    @Override
    public OaPaymentProgressVo queryById(Long paymentProgressId){
        return baseMapper.selectVoById(paymentProgressId);
    }

    /**
     * 查询项目付款进度列表
     */
    @Override
    public TableDataInfo<OaPaymentProgressVo> queryPageList(OaPaymentProgressBo bo, PageQuery pageQuery) {
        QueryWrapper<OaPaymentProgress> lqw = buildQueryWrapper(bo);
        Page<OaPaymentProgressVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目付款进度列表
     */
    @Override
    public List<OaPaymentProgressVo> queryList(OaPaymentProgressBo bo) {
        QueryWrapper<OaPaymentProgress> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaPaymentProgress> buildQueryWrapper(OaPaymentProgressBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaPaymentProgress> lqw = Wrappers.query();
        lqw.like(bo.getProjectId() != null,"opp.project_id", bo.getProjectId());
        lqw.ge(bo.getStartTime() != null, "opp.start_time", bo.getStartTime());
        lqw.le(bo.getEndTime() != null,"opp.end_time", bo.getEndTime());
        lqw.eq(bo.getIsVoid() != null, "opp.is_void", bo.getIsVoid());
        lqw.eq(bo.getProjectName()!=null,"sop.project_name",bo.getProjectName());
        lqw.eq(bo.getProjectNum()!=null,"sop.project_num",bo.getProjectNum());
        lqw.eq(bo.getProjectCode()!=null,"sop.project_code",bo.getProjectCode());
        lqw.eq("opp.del_flag",0);
        return lqw;
    }

    /**
     * 新增项目付款进度
     */
    @Override
    public Boolean insertByBo(OaPaymentProgressBo bo) {
        OaPaymentProgress add = BeanUtil.toBean(bo, OaPaymentProgress.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPaymentProgressId(add.getPaymentProgressId());
        }
        return flag;
    }

    /**
     * 修改项目付款进度
     */
    @Override
    public Boolean updateByBo(OaPaymentProgressBo bo) {
        OaPaymentProgress update = BeanUtil.toBean(bo, OaPaymentProgress.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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