package cn.iocoder.yudao.module.mes.dal.mysql.organization;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.dal.dataobject.stock.ErpStockOutItemDO;
import cn.iocoder.yudao.module.mes.controller.admin.feedingrecord.vo.FeedingRecordRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.organization.vo.OrganizationListReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.organization.OrganizationDO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;

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

/**
 * 产线工位 Mapper
 *
 * @author 内蒙必硕
 */
@Mapper
public interface OrganizationMapper extends BaseMapperX<OrganizationDO> {

    default List<OrganizationDO> selectList(OrganizationListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .likeIfPresent(OrganizationDO::getName, reqVO.getName())
                .eqIfPresent(OrganizationDO::getParentId, reqVO.getParentId())
                .eqIfPresent(OrganizationDO::getSort, reqVO.getSort())
                .eqIfPresent(OrganizationDO::getWorkerUserId, reqVO.getWorkerUserId())
                .eqIfPresent(OrganizationDO::getMachineId, reqVO.getMachineId())
                .eqIfPresent(OrganizationDO::getPhone, reqVO.getPhone())
                .eqIfPresent(OrganizationDO::getEmail, reqVO.getEmail())
                .eqIfPresent(OrganizationDO::getIsEnable, reqVO.getIsEnable())
                .eqIfPresent(OrganizationDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrganizationDO::getOrgClass, reqVO.getOrgClass())
                .betweenIfPresent(OrganizationDO::getCreateTime, reqVO.getCreateTime())
                //查询工序类型时把父级类型也查上，工厂，车间，产线等父级类型一般为空
                .and(!StringUtils.isEmpty(reqVO.getOrgType()),
                        i -> i.eq(OrganizationDO::getOrgType,reqVO.getOrgType())
                                .or().isNull(OrganizationDO::getOrgType)
                )
                .orderByAsc(OrganizationDO::getSort));
    }
    default List<OrganizationDO> selectList2(OrganizationListReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<OrganizationDO>()
                .likeIfPresent(OrganizationDO::getName, reqVO.getName())
                .eqIfPresent(OrganizationDO::getParentId, reqVO.getParentId())
                .eqIfPresent(OrganizationDO::getSort, reqVO.getSort())
                .eqIfPresent(OrganizationDO::getWorkerUserId, reqVO.getWorkerUserId())
                .eqIfPresent(OrganizationDO::getMachineId, reqVO.getMachineId())
                .eqIfPresent(OrganizationDO::getPhone, reqVO.getPhone())
                .eqIfPresent(OrganizationDO::getEmail, reqVO.getEmail())
                .eqIfPresent(OrganizationDO::getOrgType,reqVO.getOrgType())
                .eqIfPresent(OrganizationDO::getIsEnable, reqVO.getIsEnable())
                .eqIfPresent(OrganizationDO::getStatus, reqVO.getStatus())
                .eqIfPresent(OrganizationDO::getOrgClass, reqVO.getOrgClass())
                .betweenIfPresent(OrganizationDO::getCreateTime, reqVO.getCreateTime())
                .orderByAsc(OrganizationDO::getSort));
    }
    default OrganizationDO selectByParentIdAndName(Long parentId, String name) {
        return selectOne(OrganizationDO::getParentId, parentId, OrganizationDO::getName, name);
    }

    default Long selectCountByParentId(Long parentId) {
        return selectCount(OrganizationDO::getParentId, parentId);
    }
    default OrganizationDO selectByNo(String no) {
        return selectOne(OrganizationDO::getCode, no);
    }

    default List<OrganizationDO> selectListByOutIds(Collection<Long> outIds) {
        return selectList(OrganizationDO::getId, outIds);
    }

}