package cn.iocoder.yudao.module.pms.dal.mysql.iotrydailyreportdetail;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pms.controller.admin.iotrydailyreportdetail.vo.IotRyDailyReportDetailPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotrydailyreportdetail.IotRyDailyReportDetailDO;
import org.apache.ibatis.annotations.Mapper;

import java.time.format.DateTimeFormatter;

/**
 * 瑞鹰日报明细(生产动态拆分) Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotRyDailyReportDetailMapper extends BaseMapperX<IotRyDailyReportDetailDO> {

    default PageResult<IotRyDailyReportDetailDO> selectPage(IotRyDailyReportDetailPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotRyDailyReportDetailDO>()
                .eqIfPresent(IotRyDailyReportDetailDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotRyDailyReportDetailDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(IotRyDailyReportDetailDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(IotRyDailyReportDetailDO::getReportId, reqVO.getReportId())
                .inIfPresent(IotRyDailyReportDetailDO::getReportId, reqVO.getReportIds())
                .betweenIfPresent(IotRyDailyReportDetailDO::getReportDate, reqVO.getReportDate())
                .betweenIfPresent(IotRyDailyReportDetailDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(IotRyDailyReportDetailDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(IotRyDailyReportDetailDO::getDuration, reqVO.getDuration())
                .eqIfPresent(IotRyDailyReportDetailDO::getCurrentDepth, reqVO.getCurrentDepth())
                .eqIfPresent(IotRyDailyReportDetailDO::getConstructionDetail, reqVO.getConstructionDetail())
                .eqIfPresent(IotRyDailyReportDetailDO::getCurrentOperation, reqVO.getCurrentOperation())
                .eqIfPresent(IotRyDailyReportDetailDO::getSort, reqVO.getSort())
                .eqIfPresent(IotRyDailyReportDetailDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotRyDailyReportDetailDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(IotRyDailyReportDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotRyDailyReportDetailDO::getId));
    }

    default PageResult<IotRyDailyReportDetailDO> selectPages(IotRyDailyReportDetailPageReqVO reqVO) {
        LambdaQueryWrapperX<IotRyDailyReportDetailDO> queryWrapper = new LambdaQueryWrapperX<IotRyDailyReportDetailDO>();
        String dateStr = ObjUtil.isNotEmpty(reqVO.getFillOrderReportDate()) ? reqVO.getFillOrderReportDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : StrUtil.EMPTY;

        queryWrapper
                .eqIfPresent(IotRyDailyReportDetailDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotRyDailyReportDetailDO::getProjectId, reqVO.getProjectId())
                .eqIfPresent(IotRyDailyReportDetailDO::getTaskId, reqVO.getTaskId())
                .eqIfPresent(IotRyDailyReportDetailDO::getReportId, reqVO.getReportId())
                .inIfPresent(IotRyDailyReportDetailDO::getReportId, reqVO.getReportIds())
                .betweenIfPresent(IotRyDailyReportDetailDO::getReportDate, reqVO.getReportDate())
                .betweenIfPresent(IotRyDailyReportDetailDO::getStartTime, reqVO.getStartTime())
                .betweenIfPresent(IotRyDailyReportDetailDO::getEndTime, reqVO.getEndTime())
                .eqIfPresent(IotRyDailyReportDetailDO::getDuration, reqVO.getDuration())
                .eqIfPresent(IotRyDailyReportDetailDO::getCurrentDepth, reqVO.getCurrentDepth())
                .eqIfPresent(IotRyDailyReportDetailDO::getConstructionDetail, reqVO.getConstructionDetail())
                .eqIfPresent(IotRyDailyReportDetailDO::getCurrentOperation, reqVO.getCurrentOperation())
                .eqIfPresent(IotRyDailyReportDetailDO::getSort, reqVO.getSort())
                .eqIfPresent(IotRyDailyReportDetailDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotRyDailyReportDetailDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(IotRyDailyReportDetailDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotRyDailyReportDetailDO::getId);

        if (ObjUtil.isNotEmpty(reqVO.getFillOrderReportDate())) {
            queryWrapper.apply("DATE(report_date) = {0}", dateStr);
        }

        return selectPage(reqVO, queryWrapper);
    }

}