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.controller.admin.erp.vo.ErpMaterialPageReqVO;
import com.zt.plat.module.erp.dal.dataobject.erp.ErpMaterialDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * ERP物料数据 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface ErpMaterialMapper extends BaseMapperX<ErpMaterialDO> {

    default PageResult<ErpMaterialDO> selectPage(ErpMaterialPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ErpMaterialDO>()
                .likeIfPresent(ErpMaterialDO::getDownCenterNumber, reqVO.getDownCenterNumber())
                .eqIfPresent(ErpMaterialDO::getCenterNumber, reqVO.getCenterNumber())
                .betweenIfPresent(ErpMaterialDO::getCreateDate, reqVO.getCreateDate())
                .eqIfPresent(ErpMaterialDO::getMaterialType, reqVO.getMaterialType())
                .betweenIfPresent(ErpMaterialDO::getMaterialGroupDate, reqVO.getMaterialGroupDate())
                .betweenIfPresent(ErpMaterialDO::getExternalMaterialGroupDate, reqVO.getExternalMaterialGroupDate())
                .eqIfPresent(ErpMaterialDO::getUnit, reqVO.getUnit())
                .likeIfPresent(ErpMaterialDO::getUnitDescription, reqVO.getUnitDescription())
                .likeIfPresent(ErpMaterialDO::getMaterialTypeDescription, reqVO.getMaterialTypeDescription())
                .likeIfPresent(ErpMaterialDO::getMaterialGroupDescription, reqVO.getMaterialGroupDescription())
                .eqIfPresent(ErpMaterialDO::getExternalMaterialGroupDescription, reqVO.getExternalMaterialGroupDescription())
                .likeIfPresent(ErpMaterialDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(ErpMaterialDO::getMaterialLengthDescription, reqVO.getMaterialLengthDescription())
                .orderByDesc(ErpMaterialDO::getId));
    }

    void updateBatchByNumber(@Param("toUpdate") List<ErpMaterialDO> toUpdate);

    String selectMaxCode();

    Integer countByErpMNumbers(List<String> erpMNumber);

    default String getMaterialUnit(String downCenterNumber) {
        return selectOne(new LambdaQueryWrapperX<ErpMaterialDO>()
                .eq(ErpMaterialDO::getDownCenterNumber, downCenterNumber)
                .last("limit 1")).getUnit();
    }
}