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

import cn.iocoder.yudao.framework.common.pojo.PageParam;
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.mold.vo.MoldPageReqVO;
import cn.iocoder.yudao.module.common.dal.dataobject.mold.MoldDO;
import com.alibaba.excel.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;

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

/**
 * 模具 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface MoldMapper extends BaseMapperX<MoldDO> {

    default PageResult<MoldDO> selectPage(MoldPageReqVO reqVO) {
        LambdaQueryWrapperX<MoldDO> wrapper = new LambdaQueryWrapperX<>();
        wrapper
                .likeIfPresent(MoldDO::getCode, reqVO.getCode())
                .likeIfPresent(MoldDO::getName, reqVO.getName())
                .eqIfPresent(MoldDO::getUnitId, reqVO.getUnitId())
                .eqIfPresent(MoldDO::getMachineId, reqVO.getMachineId())
                .likeIfPresent(MoldDO::getMachineName, reqVO.getMachineName())
                .eqIfPresent(MoldDO::getUseTime, reqVO.getUseTime())
                .betweenIfPresent(MoldDO::getInTime, reqVO.getInTime())
                .eqIfPresent(MoldDO::getStatus, reqVO.getStatus())
                .likeIfPresent(MoldDO::getImages, reqVO.getImages())
                .likeIfPresent(MoldDO::getRemark, reqVO.getRemark())
                .eqIfPresent(MoldDO::getIsEnable, reqVO.getIsEnable())
                .betweenIfPresent(MoldDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(MoldDO::getBrandId, reqVO.getBrandId())
                .eqIfPresent(MoldDO::getStatus, reqVO.getStatus())
                .orderByDesc(MoldDO::getId);

        if (StringUtils.isNotBlank(reqVO.getIds())) {
            List<Long> idList = Arrays.stream(reqVO.getIds().split(","))
                    .map(String::trim)
                    .map(Long::valueOf)
                    .collect(Collectors.toList());
            wrapper.in(MoldDO::getId, idList);
        }

        if (reqVO.getStatuss() != null && !reqVO.getStatuss().isEmpty()) {
            wrapper.in(MoldDO::getStatus, reqVO.getStatuss());
        }
        return selectPage(reqVO, wrapper);
    }

    // 保持原有方法以兼容旧代码
    default PageResult<MoldDO> selectPage(PageParam reqVO, Long brandId) {
        return selectPage(reqVO, new LambdaQueryWrapperX<MoldDO>()
                .eqIfPresent(MoldDO::getBrandId, brandId)
                .orderByDesc(MoldDO::getId));
    }

    default int deleteByBrandId(Long brandId) {
        return delete(MoldDO::getBrandId, brandId);
    }

    default List<MoldDO> selectBy(MoldDO reqVO) {
        return selectList(new LambdaQueryWrapperX<MoldDO>()
                .eqIfPresent(MoldDO::getCode, reqVO.getCode())
                .likeIfPresent(MoldDO::getName, reqVO.getName())
                .eqIfPresent(MoldDO::getBrandId, reqVO.getBrandId())
                .orderByDesc(MoldDO::getId));
    }

    default List<MoldDO> selectBy(Long brandId) {
        return selectList(new LambdaQueryWrapperX<MoldDO>()
                .eq(MoldDO::getBrandId, brandId)
                .orderByDesc(MoldDO::getId));
    }
}