package cn.iocoder.yudao.module.pms.dal.mysql.iotmainworkorderbommaterial;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialPageReqVO;
import cn.iocoder.yudao.module.pms.controller.admin.iotmainworkorderbommaterial.vo.IotMainWorkOrderBomMaterialRespVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotmainworkorderbommaterial.IotMainWorkOrderBomMaterialDO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;

/**
 * PMS 保养工单明细设备BOM挂载物料关联 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotMainWorkOrderBomMaterialMapper extends BaseMapperX<IotMainWorkOrderBomMaterialDO> {

    default PageResult<IotMainWorkOrderBomMaterialDO> selectPage(IotMainWorkOrderBomMaterialPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotMainWorkOrderBomMaterialDO>()
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMainPlanId, reqVO.getMainPlanId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getWorkOrderId, reqVO.getWorkOrderId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMainWorkOrderDetailId, reqVO.getMainWorkOrderDetailId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getBomNodeId, reqVO.getBomNodeId())
                .likeIfPresent(IotMainWorkOrderBomMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getCode, reqVO.getCode())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialId, reqVO.getMaterialId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialName, reqVO.getMaterialName())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getKilometerCycle, reqVO.getKilometerCycle())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getKiloCycleLead, reqVO.getKiloCycleLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTimePeriod, reqVO.getTimePeriod())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTimePeriodLead, reqVO.getTimePeriodLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getNaturalDatePeriod, reqVO.getNaturalDatePeriod())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getNaturalDatePeriodLead, reqVO.getNaturalDatePeriodLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialSource, reqVO.getMaterialSource())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getInventoryAddress, reqVO.getInventoryAddress())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotMainWorkOrderBomMaterialDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotMainWorkOrderBomMaterialDO::getId));
    }

    default List<IotMainWorkOrderBomMaterialDO> selectList(IotMainWorkOrderBomMaterialPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotMainWorkOrderBomMaterialDO>()
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMainPlanId, reqVO.getMainPlanId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getWorkOrderId, reqVO.getWorkOrderId())
                .inIfPresent(IotMainWorkOrderBomMaterialDO::getWorkOrderId, reqVO.getWorkOrderIds())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMainWorkOrderDetailId, reqVO.getMainWorkOrderDetailId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getDeviceCategoryId, reqVO.getDeviceCategoryId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getBomNodeId, reqVO.getBomNodeId())
                .inIfPresent(IotMainWorkOrderBomMaterialDO::getBomNodeId, reqVO.getBomNodeIds())
                .likeIfPresent(IotMainWorkOrderBomMaterialDO::getName, reqVO.getName())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getCode, reqVO.getCode())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialId, reqVO.getMaterialId())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialName, reqVO.getMaterialName())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTotalPrice, reqVO.getTotalPrice())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getKilometerCycle, reqVO.getKilometerCycle())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getKiloCycleLead, reqVO.getKiloCycleLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTimePeriod, reqVO.getTimePeriod())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getTimePeriodLead, reqVO.getTimePeriodLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getNaturalDatePeriod, reqVO.getNaturalDatePeriod())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getNaturalDatePeriodLead, reqVO.getNaturalDatePeriodLead())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getMaterialSource, reqVO.getMaterialSource())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getInventoryAddress, reqVO.getInventoryAddress())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getSort, reqVO.getSort())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotMainWorkOrderBomMaterialDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(IotMainWorkOrderBomMaterialDO::getCreateTime, reqVO.getCreateTime()));
    }

    @DataPermission(enable = false)
    IPage<IotMainWorkOrderBomMaterialRespVO> selectMaterialsBySapOrg(IPage<IotMainWorkOrderBomMaterialRespVO> page,
                                                                   @Param("parentDept") Long parentDept, @Param("materialCodes") List<String> materialCodes,
                                                                     @Param("factoryIds") Set<Long> factoryIds,
                                                                     @Param("costCenterIds") Set<Long> costCenterIds,
                                                                     @Param("stockLocationIds") Set<Long> stockLocationIds,
                                                                     @Param("materialName") String materialName,
                                                                     @Param("materialCode") String materialCode);

    @Update("UPDATE rq_iot_main_work_order_bom_material SET deleted = 1, update_time = #{deletedTime} WHERE work_order_id = #{workOrderId}")
    void updateToDelete(@Param("workOrderId") Long workOrderId, @Param("deletedTime") LocalDateTime deletedTime);
}