package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.helper.LoginHelper;
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.OaProjectReportBo;
import com.ruoyi.oa.domain.vo.OaProjectReportVo;
import com.ruoyi.oa.domain.OaProjectReport;
import com.ruoyi.oa.mapper.OaProjectReportMapper;
import com.ruoyi.oa.service.IOaProjectReportService;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 项目报工Service业务层处理
 *
 * @author hdka
 * @date 2025-06-16
 */
@RequiredArgsConstructor
@Service
public class OaProjectReportServiceImpl implements IOaProjectReportService {

    private final OaProjectReportMapper baseMapper;

    /**
     * 查询项目报工
     */
    @Override
    public OaProjectReportVo queryById(Long reportId){
        return baseMapper.selectVoByIdPlus(reportId);
    }

    /**
     * 查询项目报工列表
     */
    @Override
    public TableDataInfo<OaProjectReportVo> queryPageList(OaProjectReportBo bo, PageQuery pageQuery) {
        QueryWrapper<OaProjectReport> lqw = buildQueryWrapper(bo);
        Page<OaProjectReportVo> result = baseMapper.selectVoPagePlus(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询项目报工列表
     */
    @Override
    public List<OaProjectReportVo> queryList(OaProjectReportBo bo) {
        QueryWrapper<OaProjectReport> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<OaProjectReport> buildQueryWrapper(OaProjectReportBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<OaProjectReport> lqw = Wrappers.query();
        lqw.eq(bo.getUserId()!=null, "opr.user_id", bo.getUserId());

        lqw.eq(bo.getProjectId() != null, "opr.project_id", bo.getProjectId());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), "su.nick_name", bo.getNickName());
        lqw.like(StringUtils.isNotBlank(bo.getProjectNum()), "op.project_num", bo.getProjectNum());
        lqw.like(StringUtils.isNotBlank(bo.getProjectCode()), "op.project_code", bo.getProjectCode());
        lqw.eq(bo.getDeptId()!=null, "su.dept_id", bo.getDeptId());
        if (bo.getCreateTime() != null) {
            LocalDateTime start = bo.getCreateTime()            // LocalDateTime / Date 都行
                    .toInstant()
                    .atZone(ZoneId.systemDefault())
                    .toLocalDate()               // 先取日期
                    .atStartOfDay();             // 00:00:00
            LocalDateTime end   = start.plusDays(1);            // 下一天 00:00:00

            lqw.ge("opr.create_time", start)
                    .lt("opr.create_time", end);                     // [start, end)
        }
        return lqw;
    }

    /**
     * 新增项目报工
     */
    @Override
    public Boolean insertByBo(OaProjectReportBo bo) {
        OaProjectReport add = BeanUtil.toBean(bo, OaProjectReport.class);
        validEntityBeforeSave(add);
        add.setUserId(LoginHelper.getUserId());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setReportId(add.getReportId());
        }
        return flag;
    }

    /**
     * 修改项目报工
     */
    @Override
    public Boolean updateByBo(OaProjectReportBo bo) {
        OaProjectReport update = BeanUtil.toBean(bo, OaProjectReport.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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