package cn.iocoder.yudao.module.common.dal.mysql.moldrepair;

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.common.controller.admin.moldrepair.vo.MoldRepairPageReqVO;
import cn.iocoder.yudao.module.common.dal.dataobject.moldrepair.MoldRepairDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 设备维修记录 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
@Repository
public interface MoldRepairMapper extends BaseMapperX<MoldRepairDO> {

    default PageResult<MoldRepairDO> selectPage(MoldRepairPageReqVO reqVO) {


        LambdaQueryWrapperX<MoldRepairDO> moldRepairDOLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        moldRepairDOLambdaQueryWrapperX
                .eqIfPresent(MoldRepairDO::getRepairCode, reqVO.getRepairCode())
                .likeIfPresent(MoldRepairDO::getRepairName, reqVO.getRepairName())
                .eqIfPresent(MoldRepairDO::getMoldId, reqVO.getMoldId())
                .eqIfPresent(MoldRepairDO::getMoldCode, reqVO.getMoldCode())
                .likeIfPresent(MoldRepairDO::getMoldName, reqVO.getMoldName())
                .eqIfPresent(MoldRepairDO::getMoldBrand, reqVO.getMoldBrand())
                .eqIfPresent(MoldRepairDO::getMoldSpec, reqVO.getMoldSpec())
                .eqIfPresent(MoldRepairDO::getMoldTypeId, reqVO.getMoldTypeId())
                .betweenIfPresent(MoldRepairDO::getRequireDate, reqVO.getRequireDate())
                .betweenIfPresent(MoldRepairDO::getFinishDate, reqVO.getFinishDate())
                .betweenIfPresent(MoldRepairDO::getConfirmDate, reqVO.getConfirmDate())
                .eqIfPresent(MoldRepairDO::getRepairResult, reqVO.getRepairResult())
                .eqIfPresent(MoldRepairDO::getAcceptedBy, reqVO.getAcceptedBy())
                .eqIfPresent(MoldRepairDO::getConfirmBy, reqVO.getConfirmBy())
                .eqIfPresent(MoldRepairDO::getStatus, reqVO.getStatus())
                .eqIfPresent(MoldRepairDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MoldRepairDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MoldRepairDO::getCreateTime);

        // 单独处理 ids 条件
        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            moldRepairDOLambdaQueryWrapperX.in(MoldRepairDO::getId, idList);
        }

        return selectPage(reqVO,moldRepairDOLambdaQueryWrapperX );
    }

}