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.OaReportScheduleBo;
import com.ruoyi.oa.domain.vo.OaReportScheduleVo;
import com.ruoyi.oa.domain.OaReportSchedule;
import com.ruoyi.oa.mapper.OaReportScheduleMapper;
import com.ruoyi.oa.service.IOaReportScheduleService;

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

/**
 * 项目排产Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-15
 */
@RequiredArgsConstructor
@Service
public class OaReportScheduleServiceImpl implements IOaReportScheduleService {

    private final OaReportScheduleMapper baseMapper;

    /**
     * 查询项目排产
     */
    @Override
    public OaReportScheduleVo queryById(Long scheduleId){
        return baseMapper.selectVoById(scheduleId);
    }

    /**
     * 查询项目排产列表
     */
    @Override
    public TableDataInfo<OaReportScheduleVo> queryPageList(OaReportScheduleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<OaReportSchedule> lqw = buildQueryWrapper(bo);
        Page<OaReportScheduleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目排产列表
     */
    @Override
    public List<OaReportScheduleVo> queryList(OaReportScheduleBo bo) {
        LambdaQueryWrapper<OaReportSchedule> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<OaReportSchedule> buildQueryWrapper(OaReportScheduleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<OaReportSchedule> lqw = Wrappers.lambdaQuery();

        // 其他条件保持不变
        lqw.eq(bo.getProjectId() != null, OaReportSchedule::getProjectId, bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getScheduleName()), OaReportSchedule::getScheduleName, bo.getScheduleName());
        lqw.eq(bo.getType() != null, OaReportSchedule::getType, bo.getType());

        // 将等于查询改为范围查询（startDate <= 记录的开始时间 <= endDate）
        if (bo.getStartDate() != null && bo.getEndDate() != null) {
            lqw.between(OaReportSchedule::getStartDate, bo.getStartDate(), bo.getEndDate())
                    .between(OaReportSchedule::getEndDate, bo.getStartDate(), bo.getEndDate());
        } else if (bo.getStartDate() != null) {
            lqw.ge(OaReportSchedule::getStartDate, bo.getStartDate()); // 大于等于开始时间
        } else if (bo.getEndDate() != null) {
            lqw.le(OaReportSchedule::getEndDate, bo.getEndDate()); // 小于等于结束时间
        }

        // 其他条件保持不变
        lqw.eq(bo.getAmount() != null, OaReportSchedule::getAmount, bo.getAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getHeader()), OaReportSchedule::getHeader, bo.getHeader());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), OaReportSchedule::getContactPhone, bo.getContactPhone());
        lqw.eq(bo.getDeliveryStatus() != null, OaReportSchedule::getDeliveryStatus, bo.getDeliveryStatus());
        lqw.eq(bo.getDeliveryId() != null, OaReportSchedule::getDeliveryId, bo.getDeliveryId());
        lqw.eq(bo.getDeliveryTime() != null, OaReportSchedule::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(StringUtils.isNotBlank(bo.getAccessory()), OaReportSchedule::getAccessory, bo.getAccessory());
        lqw.eq(bo.getSort() != null, OaReportSchedule::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, OaReportSchedule::getStatus, bo.getStatus());
        // 按照创建时间倒序排列
        lqw.orderByDesc(OaReportSchedule::getCreateTime);

        return lqw;
    }

    /**
     * 新增项目排产
     */
    @Override
    public Boolean insertByBo(OaReportScheduleBo bo) {
        OaReportSchedule add = BeanUtil.toBean(bo, OaReportSchedule.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScheduleId(add.getScheduleId());
        }
        return flag;
    }

    /**
     * 修改项目排产
     */
    @Override
    public Boolean updateByBo(OaReportScheduleBo bo) {
        OaReportSchedule update = BeanUtil.toBean(bo, OaReportSchedule.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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