package com.zt.plat.module.erp.dal.mysql.erp;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.erp.api.dto.ErpProductiveVersionReqDTO;
import com.zt.plat.module.erp.controller.admin.erp.vo.ErpProductiveVersionPageReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpProductiveVersionDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

/**
 * ERP生产版本 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface ErpProductiveVersionMapper extends BaseMapperX<ErpProductiveVersionDO> {

    default PageResult<ErpProductiveVersionDO> selectPage(ErpProductiveVersionPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpProductiveVersionDO>()
                .likeIfPresent(ErpProductiveVersionDO::getFactoryNumber, reqVO.getFactoryNumber())
                .likeIfPresent(ErpProductiveVersionDO::getMaterialNumber, reqVO.getMaterialNumber())
                .likeIfPresent(ErpProductiveVersionDO::getProductiveVersionNumber, reqVO.getProductiveVersionNumber())
                .likeIfPresent(ErpProductiveVersionDO::getProductiveVersionName, reqVO.getProductiveVersionName())
                .likeIfPresent(ErpProductiveVersionDO::getBomNumber, reqVO.getBomNumber())
                .eqIfPresent(ErpProductiveVersionDO::getBlineGroup, reqVO.getBlineGroup())
                .eqIfPresent(ErpProductiveVersionDO::getGroupCount, reqVO.getGroupCount())
                .orderByDesc(ErpProductiveVersionDO::getId));
    }

    void enableErpProductiveVersion(@Param("factoryNumber") String factoryNumber,@Param("materialNumber") String materialNumber);

    default String getErpProductiveVersionByFM(ErpProductiveVersionReqDTO reqDTO){
        return selectOne(new LambdaQueryWrapperX<ErpProductiveVersionDO>()
                .eq(ErpProductiveVersionDO::getFactoryNumber, reqDTO.getFactoryNumber())
                .eq(ErpProductiveVersionDO::getMaterialNumber, reqDTO.getMaterialNumber())
                .eq(ErpProductiveVersionDO::getIsEnable, 1)
                .last("LIMIT 1")).getProductiveVersionNumber();
    };
}