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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
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.iotoutbound.vo.IotOutboundPageReqVO;
import cn.iocoder.yudao.module.pms.dal.dataobject.iotoutbound.IotOutboundDO;
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;

/**
 * PMS 出库 Mapper
 *
 * @author ruiqi
 */
@Mapper
public interface IotOutboundMapper extends BaseMapperX<IotOutboundDO> {

    default PageResult<IotOutboundDO> selectPage(IotOutboundPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<IotOutboundDO>()
                .eqIfPresent(IotOutboundDO::getStorageId, reqVO.getStorageId())
                .eqIfPresent(IotOutboundDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotOutboundDO::getFactory, reqVO.getFactory())
                .eqIfPresent(IotOutboundDO::getProjectDepartment, reqVO.getProjectDepartment())
                .eqIfPresent(IotOutboundDO::getCostCenter, reqVO.getCostCenter())
                .eqIfPresent(IotOutboundDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotOutboundDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(IotOutboundDO::getMaterialGroupName, reqVO.getMaterialGroupName())
                .eqIfPresent(IotOutboundDO::getMaterialGroupId, reqVO.getMaterialGroupId())
                .eqIfPresent(IotOutboundDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotOutboundDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotOutboundDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotOutboundDO::getBomNodeId, reqVO.getBomNodeId())
                .eqIfPresent(IotOutboundDO::getType, reqVO.getType())
                .betweenIfPresent(IotOutboundDO::getDeliveryTime, reqVO.getDeliveryTime())
                .eqIfPresent(IotOutboundDO::getReason, reqVO.getReason())
                .eqIfPresent(IotOutboundDO::getSort, reqVO.getSort())
                .eqIfPresent(IotOutboundDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotOutboundDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotOutboundDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotOutboundDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotOutboundDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(IotOutboundDO::getId));
    }


    default List<IotOutboundDO> selectListByTimeAndDept(IotOutboundPageReqVO reqVO) {
        return selectList(new LambdaQueryWrapperX<IotOutboundDO>()
                .eqIfPresent(IotOutboundDO::getStorageId, reqVO.getStorageId())
                .eqIfPresent(IotOutboundDO::getDeptId, reqVO.getDeptId())
                .eqIfPresent(IotOutboundDO::getFactory, reqVO.getFactory())
                .eqIfPresent(IotOutboundDO::getProjectDepartment, reqVO.getProjectDepartment())
                .eqIfPresent(IotOutboundDO::getCostCenter, reqVO.getCostCenter())
                .eqIfPresent(IotOutboundDO::getMaterialCode, reqVO.getMaterialCode())
                .likeIfPresent(IotOutboundDO::getMaterialName, reqVO.getMaterialName())
                .likeIfPresent(IotOutboundDO::getMaterialGroupName, reqVO.getMaterialGroupName())
                .eqIfPresent(IotOutboundDO::getMaterialGroupId, reqVO.getMaterialGroupId())
                .eqIfPresent(IotOutboundDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(IotOutboundDO::getUnitPrice, reqVO.getUnitPrice())
                .eqIfPresent(IotOutboundDO::getUnit, reqVO.getUnit())
                .eqIfPresent(IotOutboundDO::getBomNodeId, reqVO.getBomNodeId())
                .eqIfPresent(IotOutboundDO::getType, reqVO.getType())
                .betweenIfPresent(IotOutboundDO::getDeliveryTime, reqVO.getDeliveryTime())
                .eqIfPresent(IotOutboundDO::getReason, reqVO.getReason())
                .eqIfPresent(IotOutboundDO::getSort, reqVO.getSort())
                .eqIfPresent(IotOutboundDO::getStatus, reqVO.getStatus())
                .eqIfPresent(IotOutboundDO::getRemark, reqVO.getRemark())
                .eqIfPresent(IotOutboundDO::getProcessInstanceId, reqVO.getProcessInstanceId())
                .eqIfPresent(IotOutboundDO::getAuditStatus, reqVO.getAuditStatus())
                .betweenIfPresent(IotOutboundDO::getCreateTime, reqVO.getCreateTime())
                .inIfPresent(IotOutboundDO::getDeptId, reqVO.getDeptIds()));
    }

    @Update("UPDATE rq_iot_outbound SET deleted = 1, update_time = #{deletedTime} WHERE biz_id = #{workOrderId} AND reason = #{reason}")
    void updateToDelete(@Param("workOrderId") Long workOrderId, @Param("deletedTime") LocalDateTime deletedTime, @Param("reason") String reason);
}
