package com.zt.plat.module.qms.office.supplier.dal.mapper;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.MPJLambdaWrapperX;
import com.zt.plat.module.qms.enums.QmsSupplierConstant;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierExtendRespVO;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierDO;
import com.zt.plat.module.qms.office.supplier.controller.vo.SupplierPageReqVO;
import com.zt.plat.module.qms.office.supplier.dal.dataobject.SupplierPropertiesDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 供应商 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface SupplierMapper extends BaseMapperX<SupplierDO> {

    default PageResult<SupplierDO> selectPage(SupplierPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<SupplierDO>()
                .likeIfPresent(SupplierDO::getName, reqVO.getName())
                .eqIfPresent(SupplierDO::getCode, reqVO.getCode())
                .eqIfPresent(SupplierDO::getType, reqVO.getType())
                .eqIfPresent(SupplierDO::getAddress, reqVO.getAddress())
                .eqIfPresent(SupplierDO::getCreditCode, reqVO.getCreditCode())
                .eqIfPresent(SupplierDO::getRegisterDate, reqVO.getRegisterDate())
                .eqIfPresent(SupplierDO::getContact, reqVO.getContact())
                .eqIfPresent(SupplierDO::getMobile, reqVO.getMobile())
                .eqIfPresent(SupplierDO::getBusinessScope, reqVO.getBusinessScope())
                .eqIfPresent(SupplierDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(SupplierDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(SupplierDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(SupplierDO::getId));
    }

    default SupplierExtendRespVO selectOneWithCertifications(Long id) {
        return selectJoinOne(SupplierExtendRespVO.class,
//                new MPJLambdaWrapperX<SupplierDO>()
//                        .selectAll(SupplierDO.class)
//                        .selectCollection(SupplierPropertiesDO.class, SupplierExtendRespVO::getSupplierPropertiesList)
//                        .leftJoin(SupplierPropertiesDO.class, SupplierPropertiesDO::getSupplierId, SupplierDO::getId)
//                        .eq(SupplierDO::getId, id)
////                        .eq(SupplierPropertiesDO::getBusinessType, QmsSupplierConstant.CERTIFICATION_KEY)
                new MPJLambdaWrapperX<SupplierDO>()
                        .selectAll(SupplierDO.class)
                        .selectCollection(SupplierPropertiesDO.class, SupplierExtendRespVO::getSupplierPropertiesList)
                        // 将关联条件写在一个 on() 方法内
                        .leftJoin(SupplierPropertiesDO.class, on -> on
                                .eq(SupplierPropertiesDO::getSupplierId, SupplierDO::getId)
                                .eq(SupplierPropertiesDO::getBusinessType, QmsSupplierConstant.CERTIFICATION_KEY)
                        ) // 注意这里移除了之前的 .eq 条件
                        .eq(SupplierDO::getId, id)
        );
    }


}